无法替换片段。没有为片段的id找到视图

时间:2013-12-10 19:27:38

标签: android android-fragments android-fragmentactivity

我有片段和artice的列表。列表中的每个元素都与文章相关联。这很好。但是当我尝试从外部活动替换ID时,我有一个错误:No view found for id for fragment

外部活动

ArticleMuralFragment newElement = new ArticleMuralFragment();
Bundle args = new Bundle();
args.putInt("ARG_POSITION", 7);
newElement.setArguments(args);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.map_container, newElement);
transaction.addToBackStack(null);
transaction.commit();

XML ArticleFragment

logcat的

12-10 20:05:28.866: E/FragmentManager(18952): No view found for id 0x7f050013 (com.urbanforms.main:id/fragment_container) for fragment ArticleMuralFragment{423009f8 #0 id=0x7f050013}
12-10 20:05:28.866: E/FragmentManager(18952): Activity state:
12-10 20:05:28.876: D/FragmentManager(18952):   Local FragmentActivity 41fb0db0 State:
12-10 20:05:28.876: D/FragmentManager(18952):     mCreated=truemResumed=true mStopped=false mReallyStopped=false
12-10 20:05:28.876: D/FragmentManager(18952):     mLoadersStarted=true
12-10 20:05:28.876: D/FragmentManager(18952):   Active Fragments in 41fb1e58:
12-10 20:05:28.876: D/FragmentManager(18952):     #0: ArticleMuralFragment{423009f8 #0 id=0x7f050013}
12-10 20:05:28.876: D/FragmentManager(18952):       mFragmentId=#7f050013 mContainerId=#7f050013 mTag=null
12-10 20:05:28.876: D/FragmentManager(18952):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=1
12-10 20:05:28.876: D/FragmentManager(18952):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
12-10 20:05:28.876: D/FragmentManager(18952):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
12-10 20:05:28.876: D/FragmentManager(18952):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
12-10 20:05:28.876: D/FragmentManager(18952):       mFragmentManager=FragmentManager{41fb1e58 in Map{41fb0db0}}
12-10 20:05:28.876: D/FragmentManager(18952):       mActivity=com.urbanforms.main.map.Map@41fb0db0
12-10 20:05:28.876: D/FragmentManager(18952):       mArguments=Bundle[{ARG_POSITION=7}]
12-10 20:05:28.876: D/FragmentManager(18952):   Added Fragments:
12-10 20:05:28.876: D/FragmentManager(18952):     #0: ArticleMuralFragment{423009f8 #0 id=0x7f050013}
12-10 20:05:28.876: D/FragmentManager(18952):   Back Stack Indices:
12-10 20:05:28.876: D/FragmentManager(18952):     #0: BackStackEntry{423016c0 #0}
12-10 20:05:28.876: D/FragmentManager(18952):   FragmentManager misc state:
12-10 20:05:28.876: D/FragmentManager(18952):     mActivity=com.urbanforms.main.map.Map@41fb0db0
12-10 20:05:28.886: D/FragmentManager(18952):     mContainer=android.support.v4.app.FragmentActivity$2@41fb2760
12-10 20:05:28.886: D/FragmentManager(18952):     mCurState=5 mStateSaved=false mDestroyed=false
12-10 20:05:28.886: D/FragmentManager(18952):   View Hierarchy:
12-10 20:05:28.886: D/FragmentManager(18952):     com.android.internal.policy.impl.PhoneWindow$DecorView{41fb3958 V.E..... ... 0,0-1280,736}
12-10 20:05:28.886: D/FragmentManager(18952):       com.android.internal.widget.ActionBarOverlayLayout{41fb3fd0 V.ED.... ... 0,0-1280,736 #1020310 android:id/action_bar_overlay_layout}
12-10 20:05:28.886: D/FragmentManager(18952):         android.widget.FrameLayout{41fb4bd8 V.E..... ... 0,108-1280,736 #1020002 android:id/content}
12-10 20:05:28.886: D/FragmentManager(18952):           android.widget.RelativeLayout{41fbaa70 V.E..... ... 0,0-1280,628}
12-10 20:05:28.886: D/FragmentManager(18952):             android.widget.FrameLayout{41fbea10 V.E..... ... 0,0-1280,628 #7f050014 app:id/map}
12-10 20:05:28.886: D/FragmentManager(18952):               android.widget.FrameLayout{41f75178 V.E..... ... 0,0-1280,628}
12-10 20:05:28.886: D/FragmentManager(18952):                 maps.ae.i{41f7ebf0 VFED..C. ... 0,0-1280,628}
12-10 20:05:28.886: D/FragmentManager(18952):                 android.widget.RelativeLayout{41fffc58 V.E..... ... 0,0-1280,628}
12-10 20:05:28.886: D/FragmentManager(18952):                   android.widget.ImageView{41ffebf0 V.ED.... ... 7,593-82,621}
12-10 20:05:28.886: D/FragmentManager(18952):                 android.widget.RelativeLayout{41f86af0 V.E..... ... 0,0-1280,628}
12-10 20:05:28.886: W/ResourceType(18952): No package identifier when getting name for resource number 0x00000002
12-10 20:05:28.886: D/FragmentManager(18952):                   android.view.View{42050988 V.ED..C. ... 1211,13-1267,69 #2}
12-10 20:05:28.886: W/ResourceType(18952): No package identifier when getting name for resource number 0x00000003
12-10 20:05:28.886: D/FragmentManager(18952):                   android.widget.RelativeLayout{41ff3180 V.E..... ... 1267,82-1267,489 #3}
12-10 20:05:28.886: D/FragmentManager(18952):                     maps.bu.a{4205c660 G.ED..C. ... 0,0-0,0}
12-10 20:05:28.886: W/ResourceType(18952): No package identifier when getting name for resource number 0x00000001
12-10 20:05:28.886: D/FragmentManager(18952):                   android.widget.LinearLayout{42047d70 V.E..... ... 1211,502-1267,608 #1}
12-10 20:05:28.886: D/FragmentManager(18952):                     android.view.View{4205eba8 V.ED..C. ... 0,0-56,53}
12-10 20:05:28.886: D/FragmentManager(18952):                     android.view.View{4204b6f8 V.ED..C. ... 0,53-56,106}
12-10 20:05:28.896: D/FragmentManager(18952):         com.android.internal.widget.ActionBarContainer{41fb4fd0 V.ED.... ... 0,33-1280,108 #1020311 android:id/action_bar_container}
12-10 20:05:28.896: D/FragmentManager(18952):           com.android.internal.widget.ActionBarView{41fb54d0 V.E..... ... 0,0-1280,75 #1020312 android:id/action_bar}
12-10 20:05:28.896: D/FragmentManager(18952):             android.widget.LinearLayout{41fb5a38 VFE...C. ... 0,0-210,75}
12-10 20:05:28.896: D/FragmentManager(18952):               com.android.internal.widget.ActionBarView$HomeView{41fb6ab8 V.E..... ... 0,0-65,75}
12-10 20:05:28.896: D/FragmentManager(18952):                 android.widget.ImageView{41fb6e48 V.ED.... ... 0,27-21,48 #102025a android:id/up}
12-10 20:05:28.896: D/FragmentManager(18952):                 android.widget.ImageView{41fb71a0 V.ED.... ... 17,16-60,59 #102002c android:id/home}
12-10 20:05:28.896: D/FragmentManager(18952):               android.widget.LinearLayout{41fb8370 V.E..... ... 65,21-210,54}
12-10 20:05:28.896: D/FragmentManager(18952):                 android.widget.TextView{41fb8680 V.ED.... ... 0,0-134,33 #1020264 android:id/action_bar_title}
12-10 20:05:28.896: D/FragmentManager(18952):                 android.widget.TextView{41fb9300 G.ED.... ... 0,0-0,0 #1020265 android:id/action_bar_subtitle}
12-10 20:05:28.896: D/FragmentManager(18952):             com.android.internal.view.menu.ActionMenuView{41ff6690 V.ED.... ... 1280,0-1280,75}
12-10 20:05:28.896: D/FragmentManager(18952):           com.android.internal.widget.ActionBarContextView{41fb98d8 G.E..... ... 0,0-0,0 #1020313 android:id/action_context_bar}
12-10 20:05:28.896: D/FragmentManager(18952):         com.android.internal.widget.ActionBarContainer{41fb9da0 G.ED.... ... 0,0-0,0 #1020314 android:id/split_action_bar}
12-10 20:05:28.896: D/AndroidRuntime(18952): Shutting down VM
12-10 20:05:28.896: W/dalvikvm(18952): threadid=1: thread exiting with uncaught exception (group=0x41801b90)
12-10 20:05:28.906: E/AndroidRuntime(18952): FATAL EXCEPTION: main
12-10 20:05:28.906: E/AndroidRuntime(18952): Process: com.urbanforms.main, PID: 18952
12-10 20:05:28.906: E/AndroidRuntime(18952): java.lang.IllegalArgumentException: No view found for id 0x7f050013 (com.urbanforms.main:id/fragment_container) for fragment ArticleMuralFragment{423009f8 #0 id=0x7f050013}
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.os.Handler.handleCallback(Handler.java:733)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.os.Looper.loop(Looper.java:137)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at android.app.ActivityThread.main(ActivityThread.java:4998)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at java.lang.reflect.Method.invoke(Method.java:515)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-10 20:05:28.906: E/AndroidRuntime(18952):    at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:1)

您正尝试将片段放入ID为:fragment_container的视图容器中:

 FragmentTransaction transaction = getSupportFragmentManager()
                                    .beginTransaction();
                            transaction.replace(R.id.fragment_container,
                                    newElement);

但你很可能没有那个id的观点。找到你希望这个片段进入的容器视图并给它android:id =“@ + id / fragment_container标签

答案 1 :(得分:1)

我想要打开的活动Fragment在XML文件中定义了片段。要替换片段,必须以编程方式定义片段

答案 2 :(得分:0)

尝试这样的事情:

class UploadNewProductFragment extends Fragment{
...
...
private void repaintThisFragment() {
    FragmentManager fm = getActivity().getSupportFragmentManager();
    Fragment newFrame = new UploadNewProductFragment();
    fm.beginTransaction().replace(R.id.fragmentContainer, newFrame).commit();
}

答案 3 :(得分:0)

您似乎在代码和Activity布局的FrameLayout ID中使用了不同的片段ID。在代码中,您正在使用" map_container"片段ID,但stacktrace说:

No view found for id 0x7f050013 (com.urbanforms.main:id/fragment_container) for fragment ArticleMuralFragment

您确定要使用相同的片段ID吗?因为我这样做时会遇到同样的问题。希望这会有所帮助:)

答案 4 :(得分:0)

当我尝试将我没有底部导航的应用程序代码粘贴到带有底部导航栏的新应用程序中时,发生了同样的错误。 (ile,new,具有导航活动的项目等)

因此,对我来说,在main_activity.xml中定义的片段具有ID nav_host_fragment(而不是在没有导航栏的原始应用中定义的ID“容器”),显然,它是行不通的我意识到...因此,我只是相应地更改了该行,即您进行交易的那一行?替换了,我用我要替换的片段的正确ID(即R.id.container到{{1 }}

希望这对您有帮助