Userinterface NullPointerException
I am having trouble with the user interface for an Activity I wrote. Basically it is a text view with a ToggleButton
at the bottom of the screen, the TextView
text is updated every X seconds while the button is toggled on. I am getting a NullPointerException
in my onCreate
, I think the UI resources are null? I have never coded a UI for Android so I am very confused but I think I have most of it correct.
Exception:
W/dalvikvm( 486): threadid=1: thread exiting with uncaught exception (group=0x400207e8)
E/AndroidRuntime( 486): FATAL EXCEPTION: main
E/AndroidRuntime( 486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.miccbull.locator.app/com.miccbull.locator.app.LocatorActivity}: java.lang.NullPointerException
E/AndroidRuntime( 486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 486): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 486): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 486): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 486): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 486): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 486): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 486): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 486): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 486): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 486): at com.miccbull.locator.app.LocatorActivity.onCreate(LocatorActivity.java:53)
E/AndroidRuntime( 486): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 486): ... 11 more
Here is my Activity's onCreate
method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locations = new L开发者_开发知识库inkedList<String>();
//---ToggleButton---
textView = (TextView) findViewById(R.id.textView1);
textView.setText("");
//---ToggleButton---
toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
toggleButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
if (((ToggleButton)v).isChecked())
LocateOn();
else
LocateOff();
}
});
setContentView(R.layout.main);
}
Here is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView android:layout_width="fill_parent"
android:id="@+id/widget54"
android:layout_height="376dp">
<TextView android:layout_width="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView"
android:id="@+id/textView1"
android:layout_height="match_parent"></TextView>
</ScrollView>
<!-- Linear view instead of scroll view. -->
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout1"
android:orientation="vertical"
android:layout_height="378dp">
<TextView android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</LinearLayout>
<ToggleButton android:text="ToggleButton"
android:layout_width="fill_parent"
android:id="@+id/toggleButton1"
android:layout_height="wrap_content"></ToggleButton>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
You need to call setContentView(R.layout.main); before you try to access any of the elements in the layout XML by the findViewById() method.
This line com.miccbull.locator.app.LocatorActivity.onCreate(LocatorActivity.java:53)
indicates which line of your original source file is causing the error. Can you tell us what code is at line 53 in LocatorActivity.java?
精彩评论