2011-07-24 38 views
0

我遇到了我编写的活动的用户界面问题。基本上它是一个文本视图,在屏幕底部有一个ToggleButtonTextView文本在按钮打开时每隔X秒更新一次。我在onCreate中获得NullPointerException,我认为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> 
+1

您试图访问尚未加载到活动中的布局的元素时,活动的默认布局不包含任何内容,因此当您尝试使用findViewByID时,它找不到任何内容。在findViewByID之后,你应该总是测试它是否返回null –

回答

3

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

0

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