I am facing a very strange issue here. In my fragment
, After I inflate
the layout
, findViewById()
method returns null sometimes. But the issue is not reproducible at all. I am getting these crash reports from Crashlytics. And this crash happened with one of the QA members also but then he was also not able to reproduce this issue.
Here is the code for my fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View customView= inflater.inflate(R.layout.custom_view, container, false);
ImageView back= (ImageView) customView.findViewById(R.id.back);
//Throwing Null Pointer Exception for the below line.
back.someFunc();
return customView;
}
XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/topics_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?attr/default_background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/back_to_top_margin_right"
android:layout_marginBottom="@dimen/back_to_top_margin_bottom"
android:src="@drawable/back"
android:visibility="gone"
android:id="@+id/back"/>
<LinearLayout
android:id="@+id/error_parent"
android:layout_width="match_parent"
android:background="?attr/default_background"
android:orientation="vertical"
android:layout_height="match_parent">
</LinearLayout>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Crash Logs
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.news.activities.NewsHomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3928)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.myapp.news.fragments.TopicsGroupFragment.onCreateView(TopicsGroupFragment.java:157)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1869)
at android.app.Activity.performCreateCommon(Activity.java:5970)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3928)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Having a really hard time figuring out why findViewById()
is returning null for that view.
Note : I have many ImageView
resources
whose id is back
but they are all in different XML files.
It may be because in OnCreateView
the view is not fully created. Try to use the following code:
/*---------- WHEN THIS FRAGMENT VIEW IS CREATING --------------------------*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getLayoutId(), container, false);
}
/*---------- WHEN FRAGMENT VIEW IS CREATED --------------------------*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView back = (ImageView) view.findViewById(R.id.back);
back.someFunc();
}
Possibly because you are calling it too early.
Wait until onFinishInflate(). Here is a sample project demonstrating a custom View accessing its contents.
https://github.com/commonsguy/cw-omnibus/tree/master/Jank/ThreePaneBC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With