疯狂... button.setOnClickListener java.lang.NullPointerException

时间:2013-11-07 03:21:23

标签: android button nullpointerexception

老实说......我对这个错误感到疯狂.. =))我会为你支付一顿晚餐..

我在一个按钮中有一个 java.lang.NullPointerException 。错误,在代码上(MainActivity,函数doReport()):

ReportDialog = new Dialog(this);        
ReportDialog.setCancelable(true);
ReportDialog.setContentView(R.layout.report);
ReportDialog.setTitle("GeoClient Report");

button_report = (Button) findViewById(R.id.button_report);

button_report.setOnClickListener(new OnClickListener() { // line 274 **ERROR** NullPointerException
@Override
public void onClick(android.view.View v) {}  });

布局:

<Button
android:id="@+id/button_report"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:text="Report" />

更多详情:

  1. 我完全处于相同的情况,并且他们正在工作......(我认为这是可点击的视图)
  2. 如果我取消注释代码button_report =(Button)findViewById(R.id.button_report);和评论// button_report.setOnClickListener(...它的工作原理!(因此,按钮在布局上找到)
  3. 布局被称为对话框,代码

    ReportDialog = new Dialog(this);
    ReportDialog.setCancelable(真); ReportDialog.setContentView(R.layout.report); ReportDialog.setTitle(“GeoClient Report”);

  4. 最后是堆栈:

    11-07 02:47:52.179: E/Teste(10730): TOAST: doLogin: A fazer Report!!!
    11-07 02:47:52.519: D/AndroidRuntime(10730): Shutting down VM
    11-07 02:47:52.519: W/dalvikvm(10730): threadid=1: thread exiting with uncaught exception (group=0x417aa930)
    11-07 02:47:52.597: E/AndroidRuntime(10730): FATAL EXCEPTION: main
    11-07 02:47:52.597: E/AndroidRuntime(10730): java.lang.NullPointerException
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.geoclient.MainActivity.doReport(MainActivity.java:274)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.geoclient.MainActivity.onOptionsItemSelected(MainActivity.java:503)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.app.Activity.onMenuItemSelected(Activity.java:2597)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1031)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:166)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.widget.AbsListView$1.run(AbsListView.java:3424)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.os.Handler.handleCallback(Handler.java:725)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.os.Handler.dispatchMessage(Handler.java:92)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.os.Looper.loop(Looper.java:137)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at android.app.ActivityThread.main(ActivityThread.java:5227)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at java.lang.reflect.Method.invokeNative(Native Method)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at java.lang.reflect.Method.invoke(Method.java:511)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    11-07 02:47:52.597: E/AndroidRuntime(10730):    at dalvik.system.NativeStart.main(Native Method)
    11-07 02:47:54.773: I/Process(10730): Sending signal. PID: 10730 SIG: 9
    

1 个答案:

答案 0 :(得分:3)

如果您的按钮位于对话框

然后button_report = (Button) findViewById(R.id.button_report); 将此行更改为

button_report = (Button)ReportDialog.findViewById(R.id.button_report);

因为它会在主布局视图中找到您的按钮,并且在主视图中找不到此Dialog的组件。所以在实例化时定义它的视图。

<强>否则

将您的点击事件更改为:

button_report = (Button)findViewById(R.id.button_report);

button_report.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {}  });

android.view.View改为仅View