我在一個按鈕上調用onClickListener。我已經做了幾次,之前已經有效。但不知何故,這次拋出NullPointerException
。我已經看過多篇文章,但我仍然沒有得到它。setOnClickListener引發NullPointerException
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
Button viewRequests = (Button) findViewById(R.id.view_requests);
viewRequests.setOnClickListener(new View.OnClickListener() { // NPException occurred here
@Override
public void onClick(View v) {
System.out.println("View request is clicked");
}
});
}
資源文件:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp" >
<Button
android:id="@+id/view_requests"
android:background="@drawable/my_button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textSize="10sp"
android:text="View Work Requests" />
</RelativeLayout>
編輯
logcat的
07-20 17:26:40.411: D/AndroidRuntime(334): Shutting down VM
07-20 17:26:40.411: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-20 17:26:40.431: E/AndroidRuntime(334): FATAL EXCEPTION: main
07-20 17:26:40.431: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rxprojects/com.rxprojects.activities.SearchActivity}: java.lang.NullPointerException
07-20 17:26:40.431: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.view.View.performClick(View.java:2485)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.view.View$PerformClick.run(View.java:9080)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.os.Handler.handleCallback(Handler.java:587)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-20 17:26:40.431: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 17:26:40.431: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
07-20 17:26:40.431: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-20 17:26:40.431: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-20 17:26:40.431: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
07-20 17:26:40.431: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
07-20 17:26:40.431: E/AndroidRuntime(334): at com.rxprojects.activities.SearchActivity.onCreate(SearchActivity.java:226)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-20 17:26:40.431: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-20 17:26:40.431: E/AndroidRuntime(334): ... 18 more
çan你發佈確切的錯誤信息? – maxko87 2012-07-20 17:16:18
如果你刪除了點擊監聽器是屏幕上顯示的按鈕?另外,爲了說明,您發佈的佈局xml文件名爲'search.xml',並位於'/ res/layout /'文件夾中? – FoamyGuy 2012-07-20 17:16:19
奇怪。任何可疑的日誌?嘗試清理並重新構建。 – 2012-07-20 17:17:29