I have been receiving occasional Crashlytics reports of a Null Pointer Exception from calls to findViewById() within my onRestart() method in my Root Activity. It happens rarely, but often enough that I want to figure out why and fix it. Clearly, I can check for a null result and not proceed, but that feels like masking an underlying issue rather than fixing it.
I searched SO and have not found anything that matches my situation. I have called setContentView() in onCreate(), I have verified the view item exists in the xml (it is fixed and should always be present), and this code works the vast majority of the time (based on my best estimate, this happens in less than 1 session per 1000 sessions).
I have not run across any documentation that states I should do anything special with the activity in onRestart to access items in the view.
Relevant code snippet:
public class RootActivity extends FragmentActivity
{
// more member variables, but this is the relevant one
private StickyListHeadersListView m_layerMenuListView;
@Override
protected void onCreate( @Nullable Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
// do some required initializations (omitted for snippet)
setContentView( R.layout.main );
// do more initializations (also omitted for snippet)
// initializations include accessing m_layerMenuListView
}
@Override
protected void onRestart()
{
super.onRestart();
// accessing this view occasionally returns null
m_layerMenuListView = (StickyListHeadersListView) findViewById( R.id.layer_menu );
// more code, but if the above returns null, it will generate the NPE
}
}
Here is a reduced snipped of main.xml showing the layer_menu is defined:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_layout">
<!--Layer menu-->
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/layer_menu"
android:layout_height="wrap_content"
android:layout_width="210dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="@dimen/layer_menu_bottom"
android:layout_marginRight="2dp"
android:layout_marginTop="@dimen/layer_menu_top"
android:visibility="invisible"
android:background="@color/darkGray_with_transparency"
android:divider="@color/white"
android:dividerHeight="1dp"
android:listSelector="@color/header_background"
android:choiceMode="multipleChoice"
/>
</RelativeLayout>
For completeness, the stack trace:
Fatal Exception: java.lang.RuntimeException: Unable to resume activity {crc.carsapp.la/crc.carsapp.activities.RootActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.NullPointerException
at crc.carsapp.activities.RootActivity.onRestart(RootActivity.java:473)
at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1181)
at android.app.Activity.performRestart(Activity.java:5192)
at android.app.Activity.performResume(Activity.java:5203)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(NativeStart.java)