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.