Jose Almeida
Jose Almeida

Reputation: 54

Crazy... button.setOnClickListener java.lang.NullPointerException

Honestly...I'm getting crazy about this error.. =)) I would pay you a dinner for this one..

I'm having a java.lang.NullPointerException in a button. The error, is on the code (MainActivity, function 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) {}  });

The layout:

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

More details:

  1. I've togglebuttons exactly in the same situation, and they're working... (view is clickable, i guess)
  2. If I leave uncommented the code button_report = (Button) findViewById(R.id.button_report); and comment // button_report.setOnClickListener(... it works! (therefore, The button is found on the layout)
  3. The layout is called as dialog, the code

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

Finally the stack:

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

Upvotes: 0

Views: 3089

Answers (1)

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

If your button is in Dialog

then button_report = (Button) findViewById(R.id.button_report); change this line to

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

As it will find your button in your main layout view nd it does not find this Dialog's component in your main view. so define its view while instantiating it.

else

Change your click event to:

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

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

chnge android.view.View to Only View

Upvotes: 3

Related Questions