嵌套片段+ FragmentStatePagerAdapter

时间:2013-07-05 11:38:40

标签: android-viewpager back-stack fragmentstatepageradapter

我尝试使用带有View-Pager的Fragment(父片段),使用FragmentStatePagerAdapter(因为动态添加片段),在视图寻呼机的不同页面中从you-tube取出视频。当您在视图寻呼机片段(嵌套片段)中单击视频管视频时,父片段将被播放相应视频管视频的另一个片段替换。

我能够在2个片段(包含view-pager的片段和包含you-tube播放器的片段)之间来回移动。但经过一些来回移动后,我的应用程序因 stackoverflow错误

而崩溃

在搜索各种博客并发布堆栈溢出后,我发现我的嵌套片段没有从后端堆栈中弹出。我已经尝试过像 -

这样的解决方案
public void onBackPressed() {
          if (!fragment.getChildFragmentManager().popBackStackImmediate()) {
                finish();
        }
    }
} 

其中fragment是包含view-pager的片段,但app仍然崩溃。

以下是我的崩溃报告和相关代码

崩溃报告 -

07-05 16:49:24.033:E / ACRA(32192):org.pckg.name致命错误:null 07-05 16:49:24.033:E / ACRA(32192):java.lang.StackOverflowError 07-05 16:49:24.033:E / ACRA(32192):在android.text.TextLine.handleText(TextLine.java:747) 07-05 16:49:24.033:E / ACRA(32192):在android.text.TextLine.handleRun(TextLine.java:898) 07-05 16:49:24.033:E / ACRA(32192):在android.text.TextLine.measureRun(TextLine.java:414) 07-05 16:49:24.033:E / ACRA(32192):在android.text.TextLine.measure(TextLine.java:293) 07-05 16:49:24.033:E / ACRA(32192):在android.text.TextLine.metrics(TextLine.java:267) 07-05 16:49:24.033:E / ACRA(32192):在android.text.Layout.getLineExtent(Layout.java:977) 07-05 16:49:24.033:E / ACRA(32192):在android.text.Layout.drawText(Layout.java:329) 07-05 16:49:24.033:E / ACRA(32192):在android.text.Layout.draw(Layout.java:205) 07-05 16:49:24.033:E / ACRA(32192):在android.text.BoringLayout.draw(BoringLayout.java:400) 07-05 16:49:24.033:E / ACRA(32192):在android.widget.TextView.onDraw(TextView.java:5030) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13712) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12650) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.widget.AbsListView.dispatchDraw(AbsListView.java:2327) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13715) 07-05 16:49:24.033:E / ACRA(32192):在android.widget.AbsListView.draw(AbsListView.java:3653) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12650) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13715) 07-05 16:49:24.033:E / ACRA(32192):在android.support.v4.view.ViewPager.draw(ViewPager.java:2153) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12650) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13715) 07-05 16:49:24.033:E / ACRA(32192):在android.widget.FrameLayout.draw(FrameLayout.java:467) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12650) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12694) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.draw(View.java:13428) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.drawChild(ViewGroup.java:2928) 07-05 16:49:24.033:E / ACRA(32192):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797) 07-05 16:49:24.033:E / ACRA(32192):在android.view.View.getDisplayList(View.java:12648)

代码 - Parent Fragment onCreate()方法 -

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "video detail frag onCreate");
    Log.d(TAG, "bundle of onCreate = " + savedInstanceState);

    if(savedInstanceState == null){
        gPageList = getFragments();
        ArrayList<String> pageTitle = generatePageTitle();
        gDetailPagerAdapter = new CustomPagerAdapter(getChildFragmentManager(), gPageList, pageTitle);
    }else{
        isCategoryTabAdded = savedInstanceState.getBoolean("isCategoryTabAdded");
        isSearchTabAdded = savedInstanceState.getBoolean("isSearchTabAdded");

    }
}

viewpager片段onItemClick()代码 -

public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
    if (gVideoList != null) {
        FragmentManager fragManager = getParentFragment().getChildFragmentManager();
        FragmentTransaction fragTrans = fragManager.beginTransaction();

        Fragment uTubeVideoFrag = FragmentYouTubeVideoPlayer.newInstance(
                position, gVideoList.get(position).gVideoId);
        fragTrans.replace(R.id.content_frame, uTubeVideoFrag);

        fragTrans.addToBackStack(null);
        fragTrans.commit();
    }
}

0 个答案:

没有答案