0

我在按钮上调用 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>

编辑

日志猫

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
4

3 回答 3

5

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

于 2012-07-20T17:18:18.807 回答
2

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

于 2012-07-20T17:20:25.217 回答
0

检查 .xml 文件和 .java 文件中 Button 的 id 是否相同。

于 2013-09-10T12:05:24.290 回答