我找到了解决该问题的方法,并修改了您的类,以使该错误不再发生。
唯一的主要区别是您应该使用a
DialogFragment而不是a
Dialog,这样您就可以访问
getChildFragmentManager()并
FragmentManager从接收正确的信息
DialogFragment。
即使您以前使用
getChildFragmentManager()过,它也来自,
MyFragment并且
PagerDialog该类不是中的子片段
MyFragment。
我已经测试了以下代码,现在应该可以正常工作。
我的碎片
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_sandbox, container, false); Button button = (Button) v.findViewById(R.id.button); button.setonClickListener(new onClickListener() { @Override public void onClick(View arg0) { DialogFragment newFragment = PagerDialog.newInstance(); newFragment.show(getChildFragmentManager(), "dialog"); } }); return v; }}
传呼对话框
public class PagerDialog extends DialogFragment { public static PagerDialog newInstance() { return new PagerDialog(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.dialog_fragment, container, false); ViewPager mViewPager = (ViewPager) v.findViewById(R.id.view_pager); MyAdapter mAdapter = new MyAdapter(getChildFragmentManager()); mViewPager.setAdapter(mAdapter); return v; } private class MyAdapter extends FragmentStatePagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int index) { return new DummyFragment(); } @Override public int getCount() { return 2; } }}
虚拟片段
public class DummyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_dummy_layout, container, false); return v; }}
fragment_sandbox.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Test Fragment Dialog Pager" android:textSize="24sp" android:padding="20dp" /></LinearLayout>
dialog_fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Dialog Title Text " android:padding="10dp" android:textColor="#333" android:textSize="24sp"/> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="200dp"/></LinearLayout>
fragment_dummy_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Dummy Text" android:textSize="24sp" android:textColor="#ff0000"/></LinearLayout>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)