我试图通过在DialogBox的xml中使用ViewPager
并在其Fragments
Adapter
来实现此目的
public class Custom_Dialog extends Dialog
{
Context context;
ViewPager viewpager;
public Custom_Dialog(Context context)
{
super(context);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.c_dialog);
viewpager = (ViewPager) findViewById(R.id.c_dialog_viewpager);
setupViewPager(viewpager);
}
private void setupViewPager(ViewPager viewpager)
{
FragmentManager manager = null;
try
{
FragmentActivity activity = (FragmentActivity) context;
manager = activity.getSupportFragmentManager();
} catch (ClassCastException ec)
{
}
ViewPagerAdapter adapter = new ViewPagerAdapter(manager);
Frag_one f1 = new Frag_one();
Frag_two f2 = new Frag_two();
adapter.addFragment(f1, "f1");
adapter.addFragment(f2, "f2");
viewpager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
@Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
@Override
public int getCount()
{
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title)
{
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position)
{
return mFragmentTitleList.get(position);
}
} }
但我收到错误
FATAL EXCEPTION: main
Process: com.test.fragmentindialog, PID: 26790
java.lang.IllegalArgumentException: No view found for id 0x7f0c0069 (com.test.fragmentindialog:id/custom_dialog_viewpager) for fragment Frag_two{65ad03a #0 id=0x7f0c0069 android:switcher:2131492969:1}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是 MainActivity
public class MainActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Custom_Dialog dialog =new Custom_Dialog(MainActivity.this);
dialog.setCancelable(false);
dialog.show();
}}
两个碎片都只是膨胀不同颜色的视图。
问题出现在viewpager.setAdapter(adapter);