当我第二次打开父片段时,我的Viewpager崩溃了

时间:2017-10-14 16:04:30

标签: android android-fragments android-viewpager

部分信息: 我有一个片段,其中包含一个包含三个片段的viewpager。我第一次打开这个片段时,viewpager工作正常。第二次我尝试打开这个片段然后崩溃跟随堆栈跟踪

注意:这发生在kitkat 19 API以上,工作正常

父片段内的ViewPager设置代码

private void setupViewPager(ViewPager viewPager) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        adapter = new ViewPagerAdapter(getChildFragmentManager());
    } else {
        adapter = new ViewPagerAdapter(getFragmentManager());
   }
    adapter.addFrag(fragment1 , "Abc");
    adapter.addFrag(fragment2, "Bcd");
    adapter.addFrag(fragment3, "Cdf");

    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);

    myTabs.setupWithViewPager(viewPager);
}

我的viewpager适配器

class ViewPagerAdapter extends android.support.v13.app.FragmentPagerAdapter {
    private final List<android.app.Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public android.app.Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }


    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(android.app.Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

以下是崩溃堆栈跟踪:

10-14 21:23:47.276 17601-17601/com.d.p E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.d.p, PID: 17601                                                     
java.lang.IllegalStateException: No activity
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
   at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1853)
   at android.app.Fragment.performActivityCreated(Fragment.java:1714)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
   at android.app.BackStackRecord.run(BackStackRecord.java:684)
   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
   at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5017)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
   at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

最终解决
这是本机片段中的错误,也支持v4。

当您通过导航抽屉或类似其他东西选择其他片段时,具有子片段的片段将被分离。所以这些子片段的fragmentManager(getChildFragmentManager())不再存在。当那些碎片返回时,它会崩溃。

所以基本上我们需要在onDetach()中清理mChildFragmentManager。 所以只需将代码放在具有嵌套片段的片段中。

public class ConvertListToArray implements CustomConverter{
    public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, 
    Class<?> destinationClass, Class<?> sourceClass) {
        if(sourceFieldValue==null)
            return null;

        if(sourceFieldValue instanceof List && ((List<?>) sourceFieldValue).size()>0){
            List<Field> listOfFields = (List<Field>)sourceFieldValue;

            String[][] destinationValue = new String[2][2];
            for (int i = 0; i<2;i++){
                 Field f = listOfFields.get(i);
                 for (int j = 0;j<f.getItems().size();j++){
                     destinationValue[i][j] = f.getItems().get(j);
                 }
             }
             return destinationValue;

         }
        return null;
    }
}

Source Google issue Tracker