用户界面NullPointerException

时间:2011-07-24 23:37:19

标签: android android-layout

我在使用我编写的Activity的用户界面时遇到问题。基本上它是一个文本视图,屏幕底部有一个ToggleButtonTextView文本在按钮打开时每隔X秒更新一次。我在NullPointerException中收到onCreate,我认为UI资源为空?我从来没有为Android编写过UI,所以我很困惑,但我认为我的大部分都是正确的。

例外:

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

这是我的活动的onCreate方法:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    locations = new LinkedList<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);
}

这是我的main.xml文件:

<?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>

2 个答案:

答案 0 :(得分:3)

你需要调用setContentView(R.layout.main);在尝试通过findViewById()方法访问布局XML中的任何元素之前。

答案 1 :(得分:0)

此行com.miccbull.locator.app.LocatorActivity.onCreate(LocatorActivity.java:53)表示原始源文件的哪一行导致错误。你能告诉我们LocatorActivity.java第53行的代码是什么吗?