我遇到了我編寫的活動的用戶界面問題。基本上它是一個文本視圖,在屏幕底部有一個ToggleButton
,TextView
文本在按鈕打開時每隔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>
您試圖訪問尚未加載到活動中的佈局的元素時,活動的默認佈局不包含任何內容,因此當您嘗試使用findViewByID時,它找不到任何內容。在findViewByID之後,你應該總是測試它是否返回null –