This is a simple activity which takes the message from user and displays it in the second activity. From nowhere I am getting a java.lang Null pointer exception.
The data in the file is as follows :
// activity_main.xml
// This is the xml file for the main activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="29dp"
android:text="Enter the message"
android:textSize="18dp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="38dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="send"
android:onClick="sendmessage" />
</RelativeLayout>
// MainActivity.java
// this is the code for the main activity which creates an intent and pass the value entered to the display activity
package com.example.simpleactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.simpleactivity.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendmessage(View view) {
EditText textmessage = (EditText) findViewById(R.id.editText1);
String message = textmessage.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
// activity_display_message.xml
// this is the xml file for the display activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:layout_marginTop="110dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
// DisplayMessageActivity.java
// this is the display activity which gets and intent and displays the extra message
package com.example.simpleactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
tv = (TextView) findViewById(R.id.textView2);
tv.setText(message);
setContentView(tv);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
}
// The logcat is as follows:
08-09 18:23:08.812: I/Choreographer(1520): Skipped 37 frames! The application may be doing too much work on its main thread.
08-09 18:23:08.942: D/gralloc_goldfish(1520): Emulator without GPU emulation detected.
08-09 18:23:13.262: D/AndroidRuntime(1520): Shutting down VM
08-09 18:23:13.262: W/dalvikvm(1520): threadid=1: thread exiting with uncaught exception
(group=0x414c4700)
08-09 18:23:13.402: E/AndroidRuntime(1520): FATAL EXCEPTION: main
08-09 18:23:13.402: E/AndroidRuntime(1520): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpleactivity/com.example.simpleactivity.DisplayMessageActivity}: java.lang.NullPointerException
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.os.Looper.loop(Looper.java:137)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-09 18:23:13.402: E/AndroidRuntime(1520): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 18:23:13.402: E/AndroidRuntime(1520): at java.lang.reflect.Method.invoke(Method.java:525)
08-09 18:23:13.402: E/AndroidRuntime(1520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-09 18:23:13.402: E/AndroidRuntime(1520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-09 18:23:13.402: E/AndroidRuntime(1520): at dalvik.system.NativeStart.main(Native Method)
08-09 18:23:13.402: E/AndroidRuntime(1520): Caused by: java.lang.NullPointerException
08-09 18:23:13.402: E/AndroidRuntime(1520): at com.example.simpleactivity.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:20)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.Activity.performCreate(Activity.java:5133)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-09 18:23:13.402: E/AndroidRuntime(1520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-09 18:23:13.402: E/AndroidRuntime(1520): ... 11 more
08-09 18:23:17.812: I/Process(1520): Sending signal. PID: 1520 SIG: 9
In your DisplayMessageActivity onCreate() method :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
tv = (TextView) findViewById(R.id.textView2);
tv.setText(message);
setContentView(tv);
}
uncomment first setContentView() and comment last setContentView() like below :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
tv = (TextView) findViewById(R.id.textView2);
tv.setText(message);
//setContentView(tv);
}
Related
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firstproject, PID: 6019
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firstproject/com.example.firstproject.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class android.webkit.WebView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class android.webkit.WebView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.firstproject.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.firstproject.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
at android.content.res.Resources.getText(Resources.java:299)
at android.content.res.Resources.getString(Resources.java:385)
at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:684)
at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:608)
at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:631)
at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:780)
at com.android.org.chromium.android_webview.AwContents.(AwContents.java:619)
at com.android.org.chromium.android_webview.AwContents.(AwContents.java:556)
at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:311)
at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:96)
at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:263)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:123)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue$1.run(WebViewChromium.java:110)
at com.android.org.chromium.base.ThreadUtils.runOnUiThread(ThreadUtils.java:144)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.addTask(WebViewChromium.java:107)
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:260)
at android
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = (WebView) findViewById(R.id.web_view) ;
final EditText editText = (EditText) findViewById(R.id.etext) ;
webView.setWebViewClient (new WebViewClient() );
Button button = (Button)findViewById(R.id.btn_go) ;
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick (View v){
webView.loadUrl(editText.getText().toString() ) ;
}
});
}
}
Here's XML code :
Here's Emulator :
I'm experimenting FMXL with NetBeans and Scene Builder, on a Ubuntu 14.04 system.
I have a very simple App with one form, in which there is a TabPane, and this one with 2 Tabs. There is no functionality besides the default tabs selection, one at a time.
When I set a method to act on a tab selection, on Scene Builder, responding to an onSelectionChanged event, and having a method on the controller related to the fxml file, an exception is raised.
Without the above code, everything works fine (that is, compiles and runs).
The error is pointed to the XML script's attribute of the tab being defined there.
This problem is occurring with NB8.1 + SC8.1.1 + JDK1.7, and has also happened with JDK1.8; also with NB8.0.2 + SC2.2.
Is this an error of mine or a bug somewhere in the tools?
Thanks in advance.
The error message:
Executing /home/pvc/projects/java-tests/JavaFX/JavaFXApplication12/dist/run1743546166/JavaFXApplication12.jar using platform /usr/jdk1.7.0_80/bin/java
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
Java Result: 1
The code:
package javafxapplication12;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author pvc
*/
public class JavaFXApplication12 extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
package javafxapplication12;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
/**
*
* #author pvc
*/
public class FXMLDocumentController implements Initializable
{
#FXML
private Tab tab1Id;
#FXML
void handleOnTabSelected(ActionEvent event) {
System.out.println("tab1 selected");
}
#Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:controller="javafxapplication12.FXMLDocumentController">
<children>
<TabPane layoutX="-15.0" layoutY="-55.0" prefHeight="200.0" prefWidth="320.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab fx:id="tab1Id" closable="false" onSelectionChanged="#handleOnTab1Selected" text="tab1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
You have a typo, having a 1v in the clauseonSelectionChanged="#handleOnTab1Selected"which does not match the declarationvoid handleOnTabSelected(ActionEvent event)`
However, the stack trace indicates that your project is not properly setup, and that the javafx.application.Application class cannot be resolved from the runtime.
I got my selves in trouble using the onSelectionChanged clause in the FXML, because these selection changed events are fired already before the initialize method of the controller has completed, and hence your local #FXML member variables have not been populated yet for use in the handler code.
That is a nuisance.
So I refrain from using event handler clauses in the FXML and in stead define appropriate listeners in the initialize method of the controller.
I am receiving "App has unfortunately stopped" error. I am using parse so I am assuming the issue lies somewhere within that.
This is the java file that I causes the error when loading
public class NewTipActivity extends Activity {
private Tip tip;
#Override
protected void onCreate(Bundle savedInstanceState) {
tip = new Tip();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
//New Tip Fragment
setContentView(R.layout.activity_new_tip);
FragmentManager manager = getFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = new NewTipFragment();
manager.beginTransaction().add(R.id.fragmentContainer, fragment)
.commit();
}
}
public Tip getCurrentTip() {
return tip;
}
}
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.holyapp.danshinn.wingman_etiquette/com.holyapp.danshinn.wingman_etiquette.NewTipActivity}:
java.lang.IllegalArgumentException: cannot setReadAccess for a user
with null id
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: cannot setReadAccess for a user with null id
at com.parse.ParseACL.setReadAccess(ParseACL.java:308)
at com.parse.ParseACL.getDefaultACL(ParseACL.java:61)
at com.parse.ParseObject.setDefaultValues(ParseObject.java:3385)
at com.parse.ParseObject.(ParseObject.java:181)
at com.parse.ParseObject.(ParseObject.java:127)
at com.holyapp.danshinn.wingman_etiquette.Tip.(Tip.java:17)
at com.holyapp.danshinn.wingman_etiquette.NewTipActivity.onCreate(NewTipActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I am trying to add items to a spinner but I get the error "Unfortunately my app has stopped working". I have created a string array in strings.xml in which I have some items. When the user selects the add item from the MainActivity an editText with a button appears and I want whatever is in the editText to be passed into the spinner. The spinner is in the activity_main.xml and I am working on addactivity.xml. How am I going to pass the new items from the EditText (in addactivity.xml and AddActivity.java) to the spinner in the MainActivity.java and activity_main.xml?
Here is my code:
public class AddActivity extends Activity{
Spinner spnr1;
EditText edtxAddActivity;
ArrayAdapter<String> spinnerAdapter;
Button btnAddActivity;
String[] actArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addactivity);
addActivityButton();
}
public void addActivityButton() {
// TODO Auto-generated method stub
edtxAddActivity = (EditText) findViewById(R.id.etAddActivity);
spnr1 = (Spinner) findViewById(R.id.spinner1);
actArray = getResources().getStringArray(R.array.activities_array);
spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,actArray);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnr1.setAdapter(spinnerAdapter);
btnAddActivity= (Button) findViewById(R.id.btAddActivity);
btnAddActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String newactivity = "" + edtxAddActivity.getText().toString();
spinnerAdapter.add(newactivity);
spinnerAdapter.notifyDataSetChanged();
}
});
}
}
This is my strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">MyAdroidApp</string>
<string name="activity_prompt">Choose an activity</string>
<string-array name="activities_array">
<item>5-a-side</item>
<item>Football</item>
<item>Basketball</item>
<item>Table Tennis</item>
<item>Add</item>
</string-array>
The errors displayed are the following.
07-24 14:08:43.397: E/AndroidRuntime(24798): FATAL EXCEPTION: main
07-24 14:08:43.397: E/AndroidRuntime(24798): Process: com.example.myAndroidApp, PID: 24798
07-24 14:08:43.397: E/AndroidRuntime(24798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myAndroidApp/com.example.myAndroidApp.AddActivity}: java.lang.NullPointerException
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.os.Handler.dispatchMessage(Handler.java:102)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.os.Looper.loop(Looper.java:136)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-24 14:08:43.397: E/AndroidRuntime(24798): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 14:08:43.397: E/AndroidRuntime(24798): at java.lang.reflect.Method.invoke(Method.java:515)
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-24 14:08:43.397: E/AndroidRuntime(24798): at dalvik.system.NativeStart.main(Native Method)
07-24 14:08:43.397: E/AndroidRuntime(24798): Caused by: java.lang.NullPointerException
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.example.myAndroidApp.AddActivity.addActivityButton(AddActivity.java:46)
07-24 14:08:43.397: E/AndroidRuntime(24798): at com.example.socialactivities.AddActivity.onCreate(AddActivity.java:33)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.Activity.performCreate(Activity.java:5231)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-24 14:08:43.397: E/AndroidRuntime(24798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-24 14:08:43.397: E/AndroidRuntime(24798): ... 11 more
The problem is that the spinnerAdapter produce a nullpointerexception because the listview is probably empty or not initialised. How can I initialise my array? My string-array is in array.xml in values.
NullPointerException at com.example.myAndroidApp.AddActivity.addActivityButton(AddActivity.java:46)
Means you are referecing and using something that is null at that moment. By the looks of it maybe it is one of your view references. My suggestion is put some debug checkpoints one each line of code and execute the application and follow step by step figuring out what is null
Take a look at R.layout.addactivity layout, you miss something , especially I think findViewById(R.id.etAddActivity); is null, means you don't have it in layout or smtg is wrong in it's namie, id or so
In JavaFX 2.2 I've been having trouble importing custom components that have a custom cell factory defined in the FXML. Lets say my custom component is the following
public class CustomComponent extends VBox{
public CustomComponent() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomComponent.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
} catch (IOException e ){
throw new RuntimeException(e);
}
}
}
And the corresponding FXML is
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<?import application.*?>
<?import application.TestFactory?>
<fx:root prefHeight="358.0" prefWidth="260.0" type="VBox" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<TableView prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<columns>
<TableColumn prefWidth="75.0" text="C1" >
<cellFactory>
<TestFactory />
</cellFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="C2" >
<cellFactory>
<TestFactory />
</cellFactory>
</TableColumn>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</fx:root>
and TestFactory is defined as
public class TestFactory<S,T> implements Callback<TableColumn<S, T>, TableCell<S, T>>{
#Override
public TableCell<S, T> call(TableColumn<S, T> param) {
return new TableCell<S,T>();
}
}
And all three of these files are in the same directory / src package.
When I jar these files together and try to import the jar into scenebuilder it will not find CustomComponent. However, if I take out the reference to the cellFactory & TestFactory it will import just fine. Looking at the jar analysis it seems to throw a ClassNotFoundException on TestFactory.
Not a Node: application/Main.class
Not a Node: application/TestFactory.class
Exception for: application/CustomComponent.class
javafx.fxml.LoadException:
unknown path:2
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.instantiateWithFXMLLoader(JarExplorer.java:105)
at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.exploreEntry(JarExplorer.java:146)
at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.explore(JarExplorer.java:65)
at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.exploreAndUpdateLibrary(LibraryFolderWatcher.java:298)
at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.runDiscovery(LibraryFolderWatcher.java:122)
at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.run(LibraryFolderWatcher.java:88)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.RuntimeException: javafx.fxml.LoadException:
file:/C:/Users/bthomas/AppData/Roaming/Scene%20Builder/Library/testing.jar!/application/CustomComponent.fxml
at application.CustomComponent.<init>(CustomComponent.java:17)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1010)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 8 more
Caused by: javafx.fxml.LoadException:
file:/C:/Users/bthomas/AppData/Roaming/Scene%20Builder/Library/testing.jar!/application/CustomComponent.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2864)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2708)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2677)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.CustomComponent.<init>(CustomComponent.java:15)
... 18 more
Caused by: java.lang.ClassNotFoundException: application.TestFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2932)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2921)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2862)
... 24 more
I assume this is some classpath issue with scene builder, but i'm at a loss. Has anyone encountered this problem before or know of a solution?
It seems the issue lies within the scenebuilder class loading environment. It is also a known bug / issue with scenebuilder.
A workaround is to propogate the classloader to the FXML loader.
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomComponent.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.setClassLoader(getClass().getClassLoader());
loader.load();
} catch (IOException e ){
throw new RuntimeException(e);
}