onAttachFragment中的java.lang.NullPointerException

时间:2013-12-09 08:49:05

标签: android android-fragments nullpointerexception android-fragmentactivity

像往常一样,我希望这不是一个愚蠢的问题,我希望有人可以帮助我。

我无法在手机或模拟器中重现此错误,因此我不确定是什么导致了ti,但我在Play商店的用户收到了下面的报告。

    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

我一直在努力解决这个问题,我无法修复它......问题似乎出现在我的onAttachFragment方法中,特别是我们在讨论mainActivity.java的第183行

这是:

    @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();
                    } 
            }
            }

        }

正如您所看到的,我添加了一些!= null内容以避免推送空片段等,但它仍然会出错。第183行是这一行:

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

提前感谢所有人

1 个答案:

答案 0 :(得分:1)

mStacks为空或mStacks.get(AppConstants.TAB_NEWS)为空。

将以下检查添加到您的代码中:

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