Question

In my android application I have one problem that I can't resolve already 3 days. I read a lot of posts and answers but still can't find a my solution.

My problem is when I press home button and return to application after long time later it crushes (NullPointerException).

This is my DiscountDataFragment

 View view = inflater.inflate(R.layout.discount_data, container, false);
        mListView = (ListView)view.findViewById(R.id.discountCardListView);
        mListView.setOnItemClickListener(this);
        View v = View.inflate(mActivity,R.layout.search_view,null);
        mListView.addHeaderView(v);
        mSearchEditText =(MGEditText)v.findViewById(R.id.searchEditText);
        mSearchEditText.addTextChangedListener(this);
        v.findViewById(R.id.iplus).setOnClickListener(this);

        return view;
    }

And this is exception that I have

 Caused by: java.lang.NullPointerException
            at android.view.LayoutInflater.from(LayoutInflater.java:210)
            at android.view.View.inflate(View.java:16459)
            at com.example.android.BS.fragments.discount.DiscountDataFragment.onCreateView(DiscountDataFragment.java:45)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1065)
            at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1844)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:519)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
            at android.app.Activity.performStart(Activity.java:5114)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

I couldn't understand why it crushes and what's the problem that application cannot inflate view after returning long time sleep.

  1. What is the problem here that crash application?
  2. What another solution can you offer for resolving this issue?
Was it helpful?

Solution

After you come back to your view, your activity may not be created yet when onCreateView() is called.

What you should do is to move all the code that depends on the activity to onActivityCreated() method. According to the documentation this method is:

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.

You can then use your activity.

Also, there's no need to pass the activity to the fragment, as you can always get it using getActivity() method. Not passing your activity will also limit your chance of creating a memory leak.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top