setOnClickListener抛出NullPointerException

时间:2012-07-20 17:14:04

标签: android nullpointerexception

我正在按钮上调用onClickListener。我已经多次这样做了,之前它已经有效了。但不知何故,这次抛出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

3 个答案:

答案 0 :(得分:5)

如果您传递的布局文件实际上是R.layout.search,请清理并重建项目。

答案 1 :(得分:2)

检查项目中是否有多个search.xml副本。也许您有纵向/横向的单独版本,或特定于不同屏幕尺寸的版本。如果这样做,所有版本必须在Button上设置android:id="@+id/view_requests"属性。

答案 2 :(得分:0)

检查.xml fime和.java文件中的Button是否具有相同的ID。