我在Dialogbox中加载viewpager,Viewpager包含不同的片段
下面是我的对话框代码
public void openFillUpInformationDialog(){
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_filling_form);
_mViewPager = (ViewPager) dialog.findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
dialog.show();
}
ViewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context=context;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
System.out.println("Form 1 will loaded");
f= LayoutOne.newInstance(_context);<------I am getting error over here
break;
case 1:
f= LayoutTwo.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
return 2;
}
}
LayoutOne.java是
public class LayoutOne extends Fragment {
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.first_form, null);
return root;
}
}
我已经搜索了很多次,但仍然无法解决我的问题,请帮助我。
first_form.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:background="@android:color/white"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Form - 1"
android:id="@+id/textView7"
android:textColor="#000000" />
</LinearLayout>
Custom_dialog_filling_form.xml 的LayoutFile是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewPager"
android:layout_marginBottom="75dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center_horizontal"
android:layout_alignParentLeft="false"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="false"
android:layout_marginTop="1134dp"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previouse"
android:id="@+id/btnPreviouse"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/btnNext"
android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>
我的LogCat详情
10-06 10:58:13.954 11897-11897/com.mapexample I/System.out: Form 1 will loaded
10-06 10:58:13.956 11897-11897/com.mapexample E/FragmentManager: No view found for id 0x7f0e006c (com.mapexample:id/viewPager) for fragment LayoutOne{12db1cc8 #1 id=0x7f0e006c android:switcher:2131624044:0}
10-06 10:58:13.956 11897-11897/com.mapexample E/FragmentManager: Activity state:
10-06 10:58:13.956 11897-11897/com.mapexample D/FragmentManager: Local FragmentActivity 14c06161 State:
10-06 10:58:13.956 11897-11897/com.mapexample D/FragmentManager: mCreated=truemResumed=true mStopped=false mReallyStopped=false
10-06 10:58:13.956 11897-11897/com.mapexample D/FragmentManager: mLoadersStarted=true
10-06 10:58:13.956 11897-11897/com.mapexample D/FragmentManager: Active Fragments in 259b3486:
10-06 10:58:13.956 11897-11897/com.mapexample D/FragmentManager: #0: NavigationDrawerFragment{32ba6147 #0 id=0x7f0e0067}
10-06 10:58:13.956 11897-11897/com.mapexample D/FragmentManager: mFragmentId=#7f0e0067 mContainerId=#0 mTag=null
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mAdded=true mRemoving=false mResumed=true mFromLayout=true mInLayout=true
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mFragmentManager=FragmentManager{259b3486 in MainActivity{14c06161}}
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mActivity=com.mapexample.MainActivity@14c06161
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mView=android.widget.LinearLayout{26e93d74 V.E..... ......ID 0,0-0,0 #7f0e0067 app:id/fragment_drawer}
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mInnerView=android.widget.LinearLayout{26e93d74 V.E..... ......ID 0,0-0,0 #7f0e0067 app:id/fragment_drawer}
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: Child FragmentManager{1dc0b29d in NavigationDrawerFragment{32ba6147}}:
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: FragmentManager misc state:
10-06 10:58:13.957 11897-11897/com.mapexample D/FragmentManager: mActivity=com.mapexample.MainActivity@14c06161
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mContainer=android.support.v4.app.Fragment$1@23e61312
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mParent=NavigationDrawerFragment{32ba6147 #0 id=0x7f0e0067}
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mCurState=5 mStateSaved=false mDestroyed=false
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: #1: LayoutOne{12db1cc8 #1 id=0x7f0e006c android:switcher:2131624044:0}
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mFragmentId=#7f0e006c mContainerId=#7f0e006c mTag=android:switcher:2131624044:0
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mState=1 mIndex=1 mWho=android:fragment:1 mBackStackNesting=0
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-06 10:58:13.958 11897-11897/com.mapexample D/FragmentManager: mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mFragmentManager=FragmentManager{259b3486 in MainActivity{14c06161}}
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mActivity=com.mapexample.MainActivity@14c06161
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: #2: LayoutTwo{34b1cae3 #2 id=0x7f0e006c android:switcher:2131624044:1}
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mFragmentId=#7f0e006c mContainerId=#7f0e006c mTag=android:switcher:2131624044:1
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mState=0 mIndex=2 mWho=android:fragment:2 mBackStackNesting=0
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mHidden=false mDetached=false mMenuVisible=false mHasMenu=false
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mRetainInstance=false mRetaining=false mUserVisibleHint=false
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: mFragmentManager=FragmentManager{259b3486 in MainActivity{14c06161}}
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: Added Fragments:
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: #0: NavigationDrawerFragment{32ba6147 #0 id=0x7f0e0067}
10-06 10:58:13.959 11897-11897/com.mapexample D/FragmentManager: #1: LayoutOne{12db1cc8 #1 id=0x7f0e006c android:switcher:2131624044:0}
10-06 10:58:13.960 11897-11897/com.mapexample D/FragmentManager: #2: LayoutTwo{34b1cae3 #2 id=0x7f0e006c android:switcher:2131624044:1}
10-06 10:58:13.960 11897-11897/com.mapexample D/FragmentManager: FragmentManager misc state:
10-06 10:58:13.960 11897-11897/com.mapexample D/FragmentManager: mActivity=com.mapexample.MainActivity@14c06161
10-06 10:58:13.960 11897-11897/com.mapexample D/FragmentManager: mContainer=android.support.v4.app.FragmentActivity$2@2a16bce0
10-06 10:58:13.960 11897-11897/com.mapexample D/FragmentManager: mCurState=5 mStateSaved=false mDestroyed=false
10-06 10:58:13.960 11897-11897/com.mapexample D/FragmentManager: View Hierarchy:
10-06 10:58:13.960 11897-11897/com.mapexample D/FragmentManager: com.android.internal.policy.impl.PhoneWindow$DecorView{e7adb99 V.E..... ... 0,0-0,0}
10-06 10:58:13.961 11897-11897/com.mapexample D/FragmentManager: android.widget.LinearLayout{a8e4e5e V.E..... ... 0,0-0,0}
10-06 10:58:13.961 11897-11897/com.mapexample D/FragmentManager: android.view.ViewStub{152c323f G.E..... ... 0,0-0,0 #1020373}
10-06 10:58:13.961 11897-11897/com.mapexample D/FragmentManager: android.widget.FrameLayout{16dac70c V.E..... ... 0,0-0,0}
10-06 10:58:13.961 11897-11897/com.mapexample D/FragmentManager: android.support.v7.internal.widget.FitWindowsLinearLayout{23149855 V.E..... ... 0,0-0,0 #7f0e0050 app:id/action_bar_root}
10-06 10:58:13.961 11897-11897/com.mapexample D/FragmentManager: android.support.v7.internal.widget.ViewStubCompat{1cecf26a G.E..... ... 0,0-0,0 #7f0e0051 app:id/action_mode_bar_stub}
10-06 10:58:13.964 11897-11897/com.mapexample D/FragmentManager: android.support.v7.internal.widget.NativeActionModeAwareLayout{2abbb35b V.E..... ... 0,0-0,0 #1020002 android:id/content}
10-06 10:58:13.964 11897-11897/com.mapexample D/FragmentManager: android.support.v4.widget.DrawerLayout{35f347f8 VFE..... ... 0,0-0,0 #7f0e0064 app:id/drawer}
10-06 10:58:13.964 11897-11897/com.mapexample D/FragmentManager: android.widget.LinearLayout{3a5364d1 V.E..... ... 0,0-0,0}
10-06 10:58:13.964 11897-11897/com.mapexample D/FragmentManager: android.support.v7.widget.Toolbar{8dfcb36 V.E..... ... 0,0-0,0 #7f0e0065 app:id/toolbar_actionbar}
10-06 10:58:13.964 11897-11897/com.mapexample D/FragmentManager: android.widget.TextView{18f82a37 V.ED.... ... 0,0-0,0}
10-06 10:58:13.964 11897-11897/com.mapexample D/FragmentManager: android.widget.FrameLayout{1c10eba4 V.E...C. ... 0,0-0,0 #7f0e0066 app:id/container}
10-06 10:58:13.964 11897-11897/com.mapexample D/FragmentManager: android.widget.LinearLayout{26e93d74 V.E..... ... 0,0-0,0 #7f0e0067 app:id/fragment_drawer}
10-06 10:58:13.965 11897-11897/com.mapexample D/FragmentManager: android.widget.RelativeLayout{3fd87d0d V.E..... ... 0,0-0,0 #7f0e007e app:id/navigationHeader}
10-06 10:58:13.965 11897-11897/com.mapexample D/FragmentManager: android.widget.ImageView{2a5564c2 V.ED.... ... 0,0-0,0}
10-06 10:58:13.965 11897-11897/com.mapexample D/FragmentManager: android.support.v7.widget.RecyclerView{3b1b32d3 VFED.VC. ... 0,0-0,0 #7f0e007f app:id/drawerList}
10-06 10:58:13.971 11897-11897/com.mapexample D/AndroidRuntime: Shutting down VM
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: FATAL EXCEPTION: main
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: Process: com.mapexample, PID: 11897
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: java.lang.IllegalArgumentException: No view found for id 0x7f0e006c (com.mapexample:id/viewPager) for fragment LayoutOne{12db1cc8 #1 id=0x7f0e006c android:switcher:2131624044:0}
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:1072)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:918)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.View.measure(View.java:17547)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.View.measure(View.java:17547)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.View.measure(View.java:17547)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.View.measure(View.java:17547)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.View.measure(View.java:17547)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1148)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-06 10:58:13.992 11897-11897/com.mapexample E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)