NullPointerException: Attempt to call virtual method 'android.content.Context android.content.ContextWrapper.getApplicationContext ()'

I am getting java.lang.NullPointerException when I try to get getApplicationContext()

. I am getting this error from users and cannot reproduce it on my phone.

Maybe I am calling getActivity().getApplicationContext()

before the full Fragment / Activity is loaded? If this is the problem, which function in the snippet can I call getActivity().getApplicationContext()

first?

Should I put getActivity().getApplicationContext()

in onResume()

, which comes last in the life cycle of the Fragment that becomes active?

Basically, I need to call it sometime after the fragment is loaded.

onCreateView in my fragment :

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    ctxt = getActivity().getApplicationContext();

    v = inflater.inflate(R.layout.fragment_test,
            container, false);

    //MODE CODE HERE THAT I AM NOT SHOWING  

    //THIS LINE BELOW IS GIVING THE NULL POINTER EXCEPTION - Am I calling getActivity().getApplicationContext() too early in the Fragment lifecycle?
    _adapter_lv_blockedApps2 = new Adapter_BlockedApps(
                    getActivity().getApplicationContext(), holder);

    return v;

}

      

Below is the stack trace I get from users phones.

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.ContextWrapper.getApplicationContext()' on a null object reference
at com.mavdev.focusoutfacebook.fragments.scheduledblocks.Fragment_scheduled_newdetail$populateLV_blockedAppsFromSystem.doInBackground(Fragment_scheduled_newdetail.java:2597)
at com.mavdev.focusoutfacebook.fragments.scheduledblocks.Fragment_scheduled_newdetail$populateLV_blockedAppsFromSystem.doInBackground(Fragment_scheduled_newdetail.java:2486)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more

      

Full function: private class populateLV_AppsFromSystem extends AsyncTask {

    @Override
    protected Void doInBackground(Void... params) {
        if (!isCancelled()) {
            ArrayList<Datamodel> holder = new ArrayList<Datamodel>();

            Map<String, Drawable> map_Apps = new HashMap<String, Drawable>();

            List<String> Apps_List = Arrays.asList(TextUtils.split(
                    ToDisplay.getApps(), ","));

            if (Apps_List != null) {
                for (String s : Apps_List) {
                    String s_appName = "";

                    try {
                        s_appName = mAppInfo.getAppNamefromPackage(s,
                                getActivity().getApplicationContext());
                    } catch (Exception e) {
                        s_appName = "Unknown";
                    }
                    Drawable d = mAppInfo.getIconDrawablefromAppName(
                            s_appName, getActivity());
                    map_Apps.put(s_appName, d); 
                }
            }

            /****************************************************
             * GETTING THE ADAPTER
             ****************************************************/
            for (Map.Entry<String, Drawable> entry : map_Apps
                    .entrySet()) {

                if (isCancelled()) {
                    return null;
                }

            }

            **//THIS IS WHERE THE APP IS CRASHING WITH NULL POINTER**  
            _adapter_lv_Apps2 = new Adapter_Apps(
                    getActivity().getApplicationContext(), holder);

        } else {
            _adapter_lv_Apps2 = null;
        }

        /****************************************************/

        return null;
    }

      

+3


source to share





All Articles