2

In the following code, I check the menuitem for android.R.id.home to handle up button clicks to finish the activity, but on a Gionee device running Android 4.2.2, this causes a NullPointerException.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

The crash happens on the finish() line. And I'm not overriding the onFinish() function.

The stacktrace is:

java.lang.NullPointerException
       at android.os.Parcel.readException(Parcel.java:1434)
       at android.os.Parcel.readException(Parcel.java:1379)
       at android.app.ActivityManagerProxy.finishActivity(ActivityManagerNative.java:2114)
       at android.app.Activity.finish(Activity.java:4165)
       at com.Valmeeki.Valmeeki.activities.ReadingRoomActivity.onOptionsItemSelected(ReadingRoomActivity.java:115)
       at android.app.Activity.onMenuItemSelected(Activity.java:2566)
       at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:325)
       at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
       at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
       at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
       at android.support.v7.internal.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:196)
       at android.view.View.performClick(View.java:4248)
       at android.view.View$PerformClick.run(View.java:17723)
       at android.os.Handler.handleCallback(Handler.java:800)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5421)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:525)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
       at dalvik.system.NativeStart.main(NativeStart.java)

Any idea what I'm doing wrong here? Is it some Android bug? Or am I safe in forgetting about this and just calling finish() within a try-catch?

As a final note, this is not a duplicate of What is a Null Pointer Exception, and how do I fix it?. I know very well what a NPE is and I've had my fair share during my life as a programmer. This question was asked only because it is thrown only on a certain device and at a seemingly improbable place for a NPE. I request the user who flagged this as a duplicate to read the question atleast once.

Community
  • 1
  • 1
Suhair Zain
  • 403
  • 4
  • 15
  • can you post whole activity code? – Uniruddh Oct 21 '15 at 06:11
  • Do you have code in the `onfinish` function or perhaps an object that the activity tries to close which does not exist anymore? – Niki van Stein Oct 21 '15 at 06:21
  • on which line the exception happens? do you use the correct id of the selected item? – Rich Oct 21 '15 at 06:25
  • 1
    This is for another person, so I cannot post the whole code, sorry. I'm not overriding the onFinish() function. So, I guess that cannot be it. It happens on the finish() line. This code works good on almost all devices. Issue has so far been reported only on this device, Gionee E7mini. – Suhair Zain Oct 21 '15 at 06:42
  • have you tried to debug it, and check what exactly is causing NPE? – user902383 Oct 21 '15 at 14:50
  • Hi @SuhairZain, have you found any details regarding this issue? – Bianca Daniciuc Jul 03 '17 at 13:18
  • Hi @ana01, unfortunately I'm no longer working on this project and since this issue was too difficult to track down and happened only on a single device, I was asked to ignore it. What happened, are you having the same problem as well? – Suhair Zain Jul 04 '17 at 10:17
  • Yes, sort of. It happened on a different device than Gionee E7mini, but it seems to have close specifications to it. I've also decided to ignore this issue, but I hoped there's a logical explanation for this. – Bianca Daniciuc Jul 05 '17 at 16:20

0 Answers0