0

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

我在按钮中有一个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 = 新对话框(此);
    ReportDialog.setCancelable(true); ReportDialog.setContentView(R.layout.report); ReportDialog.setTitle("GeoClient 报告");

最后是堆栈:

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
4

1 回答 1

3

如果您的按钮在对话框中

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

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

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

别的

将您的点击事件更改为:

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

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

更改android.view.View为仅 View

于 2013-11-07T03:35:36.447 回答