java.lang.NullPointerException inside onAttachFragment

as usual I hope this is not a dumb question and I hope someone can help me out.

I can't reproduce this error in my phone or in my simulator so I'm not sure what's causing ti, but I'm getting some reports like the one below from the users on the Play Store.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.josie.myclass/com.josie.myclass.mainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3768)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.josie.myclass.mainActivity.onAttachFragment(mainActivity.java:183)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:830)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1805)
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
    at com.josie.myclass.mainActivity.onCreate(mainActivity.java:55)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
    ... 11 more

I've been struggling with this for a while and I can't fix it... The problem seems to be in my onAttachFragment method and in particular we're talking about line 183 of mainActivity.java

Here it is:

    @Override
        public void onAttachFragment(Fragment fragment) {
            // TODO Auto-generated method stub
            if (fragment != null){
            super.onAttachFragment(fragment);
            if (fragment.getTag() != null){
                    if (fragment.getTag().equals("video_singolo")){
                                    mStacks.get(AppConstants.TAB_VIDEO).push(fragment);
                            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                            //Integer tabvideosize = mStacks.get(AppConstants.TAB_VIDEO).size();
                    } else if (fragment.getTag().equals("news_singola")){
                                    mStacks.get(AppConstants.TAB_NEWS).push(fragment);
                            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                            //Integer tabvideosize2 = mStacks.get(AppConstants.TAB_PIUVISTI).size();
                    } 
            }
            }

        }

As you can see I've added some != null stuff to avoid pushing null fragments etc, but it still gives the error. Line 183 is this one:

mStacks.get(AppConstants.TAB_NEWS).push(fragment);

Thanks in advance to everyone

Answers


Either mStacks is null or mStacks.get(AppConstants.TAB_NEWS) is null.

Add the following check to your code:

if(mStacks!=null && mStacks.get(AppConstants.TAB_NEWS)!=null ) mStacks.get(AppConstants.TAB_NEWS).push(fragment);

Need Your Help

How to write R extension using a static library

c++ r static

Hi I am trying to build an R package using a pre-compiled static library and a single header file. What is the best way to do so?

jQuery : Use an alternative to .toggle() which is deprecated

javascript jquery

I have some images with class name ".mute_btn" and when i click on them, my images source is changing :

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.