如何从Dialog添加片段

时间:2015-08-05 09:01:31

标签: android android-fragments android-dialog

如何从对话框中添加片段(不是DialogFragment)?我一直在跟踪http://developer.android.com/guide/components/fragments.html以及我在谷歌上发现的其他链接中的步骤,但它总是崩溃。如果不可能,还有其他选择吗?

更新:logcat

E/FragmentManager﹕ No view found for id 0x7f030032 (com.xxx.xxx.xxx.xxx:layout/fragment_xxx) for fragment xxxFragment{20f8e3bb #1 id=0x7f030032}
E/FragmentManager﹕ Activity state:
E/FragmentManager﹕ Local FragmentActivity 3cfe20d8 State:
E/FragmentManager﹕ mCreated=truemResumed=true mStopped=false mReallyStopped=false
E/FragmentManager﹕ mLoadersStarted=true
E/FragmentManager﹕ FragmentManager misc state:
E/FragmentManager﹕ mActivity=com.xxx.xxx.xxx.xxx@3cfe20d8
E/FragmentManager﹕ mContainer=android.support.v4.app.FragmentActivity$2@2545fc31
E/FragmentManager﹕ mCurState=5 mStateSaved=false mDestroyed=false
E/FragmentManager﹕ View Hierarchy:
E/FragmentManager﹕ com.android.internal.policy.impl.PhoneWindow$DecorView{30897f16 V.E..... ... 0,0-768,1184}
E/FragmentManager﹕ com.android.internal.widget.ActionBarOverlayLayout{30480097 V.E..... ... 0,0-768,1184 #1020378 android:id/decor_content_parent}
E/FragmentManager﹕ android.widget.FrameLayout{aa07284 V.E..... ... 0,50-768,1184 #1020002 android:id/content}
E/FragmentManager﹕ android.support.v4.widget.DrawerLayout{3f4d2a6d VFE..... F.. 0,0-768,1134 #7f080022 app:id/menu_layout}
E/FragmentManager﹕ android.widget.FrameLayout{341676a2 V.E..... ... 0,0-768,1134 #7f080023 app:id/content_frame}
E/FragmentManager﹕ android.widget.RelativeLayout{29020f33 V.E..... ... 0,0-768,1134 #7f080040 app:id/rlRootView}
E/FragmentManager﹕ xxx.xxx.xxx.view.PhotoView{af432f0 V.ED.... ... 0,0-768,1134 #7f080041 app:id/pv_background}
E/FragmentManager﹕ android.widget.LinearLayout{271f0069 V.E..... ... 0,160-768,457}
E/FragmentManager﹕ android.widget.ImageView{14895aee V.ED.... ... 345,0-422,137 #7f080042 app:id/iv_location}
E/FragmentManager﹕ android.widget.LinearLayout{1a446b8f V.E..... ... 0,147-768,297}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{7818e1c V.ED.... ... 218,6-549,80 #7f080043 app:id/tv_current_location}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{311e3a25 V.ED.... ... 271,80-497,130 #7f080044 app:id/tv_current_location_sub}
E/FragmentManager﹕ android.widget.LinearLayout{3d3437fa V.E..... ... 0,879-768,1134}
E/FragmentManager﹕ android.widget.LinearLayout{3da631ab V.E..... ... 0,76-255,139}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{38277008 V.ED.... ... 0,0-255,37}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{1b1d53a1 V.ED.... ... 0,37-255,63}
E/FragmentManager﹕ [ 08-05 18:12:10.349 26212:26212 E/FragmentManager ]
    android.view.View{a7dd9c6 V.ED.... ... 255,30-257,205}
E/FragmentManager﹕ android.widget.LinearLayout{5143d87 V.E...C. ... 257,0-512,215 #7f080045 app:id/linear_xxx_button}
E/FragmentManager﹕ android.widget.ImageView{31c784b4 V.ED.... ... 38,0-216,178 #7f080046 app:id/iv_restaurant}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{139a88dd V.ED.... ... 0,178-255,215}
E/FragmentManager﹕ android.view.View{266dcc52 V.ED.... ... 512,30-514,205}
E/FragmentManager﹕ android.widget.LinearLayout{2a6d2b23 V.E..... ... 514,76-768,139}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{13b23820 V.ED.... ... 0,0-254,37}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{38bbd5d9 V.ED.... ... 0,37-254,63}
E/FragmentManager﹕ android.widget.FrameLayout{21f85b9e I.E..... ... -480,0-0,1134 #7f080024 app:id/menu_fragment}
E/FragmentManager﹕ android.widget.LinearLayout{1cdd567f V.E..... ... 0,0-480,1134}
E/FragmentManager﹕ android.widget.ListView{1fd2b64c VFED..C. ... 0,0-480,1134 #7f0800c0 app:id/menu_list}
E/FragmentManager﹕ android.widget.RelativeLayout{b0af695 V.E..... ... 0,0-480,592 #7f0800c7 app:id/root_container}
E/FragmentManager﹕ android.widget.ImageView{170a93aa V.ED.... ... 140,0-340,140 #7f0800de app:id/iv_logo}
E/FragmentManager﹕ android.widget.ImageView{27fadb9b V.ED.... ... 170,226-310,366 #7f0800df app:id/menu_item_login_fb}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{299aeb38 V.ED.... ... 106,482-374,592 #7f0800e0 app:id/menu_item_login_text}
E/FragmentManager﹕ android.widget.RelativeLayout{ff16711 V.E..... ... 0,591-480,683 #7f0800c7 app:id/root_container}
E/FragmentManager﹕ android.widget.ImageView{1f564076 V.ED.... ... 120,21-170,71 #7f0800dc app:id/menu_item_icon}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{30a19677 V.ED.... ... 202,21-298,71 #7f0800dd app:id/menu_item_text}
E/FragmentManager﹕ android.widget.RelativeLayout{7af82e4 V.E..... ... 0,682-480,774 #7f0800c7 app:id/root_container}
E/FragmentManager﹕ [ 08-05 18:12:10.351 26212:26212 E/FragmentManager ]
    android.widget.ImageView{1ff4634d V.ED.... ... 120,21-170,71 #7f0800dc app:id/menu_item_icon}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{2bddee02 V.ED.... ... 202,21-380,71 #7f0800dd app:id/menu_item_text}
E/FragmentManager﹕ android.widget.RelativeLayout{238f2313 V.E..... ... 0,773-480,865 #7f0800c7 app:id/root_container}
E/FragmentManager﹕ android.widget.ImageView{3453e950 V.ED.... ... 120,21-170,71 #7f0800dc app:id/menu_item_icon}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{1cb0e749 V.ED.... ... 202,21-202,71 #7f0800dd app:id/menu_item_text}
E/FragmentManager﹕ android.widget.RelativeLayout{940e84e V.E..... ... 0,864-480,956 #7f0800c7 app:id/root_container}
E/FragmentManager﹕ android.widget.ImageView{31bedd6f V.ED.... ... 120,21-170,71 #7f0800dc app:id/menu_item_icon}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{8964a7c V.ED.... ... 202,21-202,71 #7f0800dd app:id/menu_item_text}
E/FragmentManager﹕ [ 08-05 18:12:10.352 26212:26212 E/FragmentManager ]
    android.widget.RelativeLayout{a97af05 V.E..... ... 0,955-480,1047 #7f0800c7 app:id/root_container}
E/FragmentManager﹕ android.widget.ImageView{1cc73b5a V.ED.... ... 120,21-170,71 #7f0800dc app:id/menu_item_icon}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{3685e18b V.ED.... ... 202,21-202,71 #7f0800dd app:id/menu_item_text}
E/FragmentManager﹕ android.widget.RelativeLayout{3e3b9268 V.E..... ... 0,1046-480,1138 #7f0800c7 app:id/root_container}
E/FragmentManager﹕ android.widget.ImageView{a693681 V.ED.... ... 120,21-170,71 #7f0800dc app:id/menu_item_icon}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{262db326 V.ED.... ... 202,21-202,71 #7f0800dd app:id/menu_item_text}
E/FragmentManager﹕ android.widget.LinearLayout{236f0b67 V.E..... ... 0,1134-480,1134 #7f0800c1 app:id/menu_logout}
E/FragmentManager﹕ com.android.internal.widget.ActionBarContainer{246b6d14 V.ED.... ... 0,50-768,146 #1020379 android:id/action_bar_container}
E/FragmentManager﹕ [ 08-05 18:12:10.352 26212:26212 E/FragmentManager ]
    com.android.internal.widget.ActionBarView{2e71b9bd V.E..... ... 0,0-768,96 #102037a android:id/action_bar}
E/FragmentManager﹕ android.widget.LinearLayout{1231dbb2 VFE...C. ... 17,0-71,96}
E/FragmentManager﹕ com.android.internal.widget.ActionBarView$HomeView{dd6f703 V.E..... ... 0,0-54,96}
E/FragmentManager﹕ android.widget.ImageView{331c4680 G.ED.... ... 0,0-0,0 #102003b android:id/up}
E/FragmentManager﹕ android.widget.ImageView{278534b9 V.ED.... ... 8,33-46,63 #102002c android:id/home}
E/FragmentManager﹕ android.widget.RelativeLayout{29de00fe V.E..... ... 112,0-656,96}
E/FragmentManager﹕ xxx.xxx.xxx.view.FontTextView{3a48005f G.ED.... ... 0,0-0,0 #7f0800c4 app:id/actionbar_title}
E/FragmentManager﹕ android.widget.LinearLayout{293f4aac V.E..... ... 214,12-329,84 #7f0800c5 app:id/actionbar_title_image}
E/FragmentManager﹕ android.widget.ImageView{5bb6375 V.ED.... ... 8,12-115,72}
E/FragmentManager﹕ android.widget.ActionMenuView{2a952f0a V.ED.... ... 656,0-768,96}
E/FragmentManager﹕ [ 08-05 18:12:10.353 26212:26212 E/FragmentManager ]
    com.android.internal.view.menu.ActionMenuItemView{196437b VF.D..CL ... 0,0-112,96 #0}
E/FragmentManager﹕ com.android.internal.widget.ActionBarContextView{21ac6598 G.E..... ... 0,0-0,0 #102037b android:id/action_context_bar}
E/FragmentManager﹕ com.android.internal.widget.ActionBarContainer{8ebc1f1 G.E..... ... 0,0-0,0 #102037c android:id/split_action_bar}
08-05 18:12:10.353  26212-26212/xxx.xxx.xxx.xxx.xxx D/AndroidRuntime﹕ Shutting down VM
E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: xxx.xxx.xxx.xxx.xxx, PID: 26212
    java.lang.IllegalArgumentException: No view found for id 0x7f030032 (xxx.xxx.xxx.xxx.xxx:layout/fragment_xxx) for fragment xxxFragment{20f8e3bb #1 id=0x7f030032}
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
            at android.app.BackStackRecord.run(BackStackRecord.java:834)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:0)

你不应该直接从对话框中添加片段,而应该调用启动此对话框的activity / fragment来代替它(可选择传递片段实例来添加)。

如果您不知道如何从对话框中传递数据,那么我的旧答案可能会对您有用:https://stackoverflow.com/a/18579638/1235698