FragmentManager:找不到ID的视图 [英] FragmentManager: No view found for id

查看:170
本文介绍了FragmentManager:找不到ID的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改 我意识到问题没有解决,所以我添加了堆栈跟踪.

Edit I realized the issue was not solved, so I added the stack trace.

在从Activity进行片段事务期间发生此错误.我阅读了先前问题的解决方案,但找不到相关案例.事务是通过适配器的回调方法完成的.我是第一次这样做,所以也许有些地方不对.如果相关,我可以添加回调代码.

This error occurs during Fragment transaction from an Activity. I read solutions for previous issues but couldn't find a relevant case. The transaction is done through a callback method from an Adapter. I'm doing it this way for the first time, so maybe something is off there. I can add the callback code if it's relevant.

最终堆栈跟踪(漆成红色的部分)

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.countriesdata, PID: 14691
    java.lang.IllegalArgumentException: No view found for id 0x7f070063 (com.example.countriesdata:id/fragments_container) for fragment CountryFragment{3ded6a8 (ab54e82e-6114-4343-90ec-ed8edf4125b5) id=0x7f070063}
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:875)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

从问题开始的堆栈跟踪

E/FragmentManager: No view found for id 0x7f070063 (com.example.countriesdata:id/fragments_container) for fragment CountryFragment{3ded6a8 (ab54e82e-6114-4343-90ec-ed8edf4125b5) id=0x7f070063}
    Activity state:
D/FragmentManager:   Local Activity cc8ca22 State:
        mResumed=true mStopped=false mFinished=false
        mChangingConfigurations=false
D/FragmentManager:     mCurrentConfig={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h659dp 420dpi nrml port finger qwerty/v/v -nav/h winConfig={ mBounds=Rect(0, 0 - 1080, 1920) mAppBounds=Rect(0, 0 - 1080, 1794) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.1}
        mLoadersStarted=true
        Active Fragments in 32b8a64:
D/FragmentManager:       #0: ReportFragment{938c5c1 #0 androidx.lifecycle.LifecycleDispatcher.report_fragment_tag}
            mFragmentId=#0 mContainerId=#0 mTag=androidx.lifecycle.LifecycleDispatcher.report_fragment_tag
            mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
            mAdded=true mRemoving=false mFromLayout=false mInLayout=false
            mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
            mRetainInstance=false mRetaining=false mUserVisibleHint=true
            mFragmentManager=FragmentManager{32b8a64 in HostCallbacks{db33166}}
            mHost=android.app.Activity$HostCallbacks@db33166
D/FragmentManager:         Child FragmentManager{93d4ca7 in ReportFragment{938c5c1}}:
              FragmentManager misc state:
                mHost=android.app.Activity$HostCallbacks@db33166
                mContainer=android.app.Fragment$1@c9eb554
                mParent=ReportFragment{938c5c1 #0 androidx.lifecycle.LifecycleDispatcher.report_fragment_tag}
                mCurState=5 mStateSaved=false mDestroyed=false
          #1: RequestManagerFragment{dcafcfd #1 com.bumptech.glide.manager}
            mFragmentId=#0 mContainerId=#0 mTag=com.bumptech.glide.manager
D/FragmentManager:         mState=5 mIndex=1 mWho=android:fragment:1 mBackStackNesting=0
            mAdded=true mRemoving=false mFromLayout=false mInLayout=false
            mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
            mRetainInstance=false mRetaining=false mUserVisibleHint=true
            mFragmentManager=FragmentManager{32b8a64 in HostCallbacks{db33166}}
            mHost=android.app.Activity$HostCallbacks@db33166
            Child FragmentManager{130fdf2 in RequestManagerFragment{dcafcfd}}:
D/FragmentManager:           FragmentManager misc state:
                mHost=android.app.Activity$HostCallbacks@db33166
                mContainer=android.app.Fragment$1@a680c43
D/FragmentManager:             mParent=RequestManagerFragment{dcafcfd #1 com.bumptech.glide.manager}
                mCurState=5 mStateSaved=false mDestroyed=false
        Added Fragments:
          #0: ReportFragment{938c5c1 #0 androidx.lifecycle.LifecycleDispatcher.report_fragment_tag}
D/FragmentManager:       #1: RequestManagerFragment{dcafcfd #1 com.bumptech.glide.manager}
        FragmentManager misc state:
          mHost=android.app.Activity$HostCallbacks@db33166
          mContainer=android.app.Activity$HostCallbacks@db33166
          mCurState=5 mStateSaved=false mDestroyed=false
      ViewRoot:
        mAdded=true mRemoved=false
        mConsumeBatchedInputScheduled=false
D/FragmentManager:     mConsumeBatchedInputImmediatelyScheduled=false
        mPendingInputEventCount=0
        mProcessInputEventsScheduled=false
        mTraversalScheduled=false    mIsAmbientMode=false
        android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
        android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
D/FragmentManager:     android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
      Choreographer:
        mFrameScheduled=false
        mLastFrameTime=69579192 (3737 ms ago)

    InsetsController:
        InsetsState
          InsetsSource type=TYPE_BOTTOM_TAPPABLE_ELEMENT frame=[0,1794][1080,1920] visible=true
          InsetsSource type=TYPE_TOP_TAPPABLE_ELEMENT frame=[0,0][1080,63] visible=true
D/FragmentManager:       InsetsSource type=TYPE_RIGHT_GESTURES frame=[1080,0][1080,1920] visible=true
          InsetsSource type=TYPE_LEFT_GESTURES frame=[0,0][0,1920] visible=true
          InsetsSource type=TYPE_BOTTOM_GESTURES frame=[0,1794][1080,1920] visible=true
          InsetsSource type=TYPE_TOP_GESTURES frame=[0,0][1080,63] visible=true
          InsetsSource type=TYPE_SIDE_BAR_1 frame=[0,1794][1080,1920] visible=true
          InsetsSource type=TYPE_TOP_BAR frame=[0,0][1080,63] visible=true
      View Hierarchy:
        DecorView@dbd2c0[ListActivity]
          android.widget.LinearLayout{84f6bf9 V.E...... ........ 0,0-1080,1794}
D/FragmentManager:         android.view.ViewStub{84d073e G.E...... ......I. 0,0-0,0 #1020194 android:id/action_mode_bar_stub}
            android.widget.FrameLayout{8f5299f V.E...... ........ 0,63-1080,1794}
              androidx.appcompat.widget.ActionBarOverlayLayout{30b5aec V.E...... ........ 0,0-1080,1731 #7f070054 app:id/decor_content_parent}
                androidx.appcompat.widget.ContentFrameLayout{11ceb5 V.E...... ........ 0,147-1080,1731 #1020002 android:id/content}
                  android.widget.RelativeLayout{98e594a V.E...... ........ 0,0-1080,1584}
                    androidx.appcompat.widget.AppCompatImageView{a0cc0bb V.ED..... ........ 0,0-1080,1584}
D/FragmentManager:                 android.widget.LinearLayout{5c939d8 V.E...... ........ 0,0-1080,1584}
                      android.widget.LinearLayout{b5da131 V.E...... ........ 0,0-1080,158}
                        androidx.appcompat.widget.AppCompatTextView{ae0c016 V.ED..... ........ 26,26-653,132}
                        androidx.appcompat.widget.AppCompatTextView{33cad97 V.ED..... ........ 705,26-1054,132}
                      androidx.recyclerview.widget.RecyclerView{7841b84 VFED..... ........ 26,184-1054,1558 #7f07008f app:id/rvId}
                        android.widget.LinearLayout{fba1f6d VFE...C.. ........ 0,0-1028,184 #7f07008e app:id/rowId}
                          android.widget.TextView{320c7a2 V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{b148c33 V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
                        android.widget.LinearLayout{ce96bf0 VFE...C.. ........ 0,187-1028,371 #7f07008e app:id/rowId}
                          android.widget.TextView{9e64569 V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{867bbee V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
                        android.widget.LinearLayout{781b88f VFE...C.. ........ 0,374-1028,558 #7f07008e app:id/rowId}
D/FragmentManager:                       android.widget.TextView{611571c V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{994cf25 V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
                        android.widget.LinearLayout{717a8fa VFE...C.. ........ 0,561-1028,745 #7f07008e app:id/rowId}
                          android.widget.TextView{5eb4eab V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{a6ac908 V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
                        android.widget.LinearLayout{ce838a1 VFE...C.. ........ 0,748-1028,932 #7f07008e app:id/rowId}
                          android.widget.TextView{da75ac6 V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{90e2a87 V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
D/FragmentManager:                     android.widget.LinearLayout{7676db4 VFE...C.. ........ 0,935-1028,1119 #7f07008e app:id/rowId}
                          android.widget.TextView{7aebddd V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{92e5d52 V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
                        android.widget.LinearLayout{898e823 VFE...C.. ........ 0,1122-1028,1306 #7f07008e app:id/rowId}
                          android.widget.TextView{de7b120 V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{25e5ad9 V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
                        android.widget.LinearLayout{eb0fc9e VFE...C.. ........ 0,1309-1028,1493 #7f07008e app:id/rowId}
                          android.widget.TextView{e87e37f V.ED..... ........ 13,13-650,171 #7f070079 app:id/name}
                          android.widget.ImageView{a66bf4c V.ED..... ........ 676,13-1015,171 #7f070061 app:id/flag}
D/FragmentManager:             androidx.appcompat.widget.ActionBarContainer{cd0cb95 V.ED..... ........ 0,0-1080,147 #7f070029 app:id/action_bar_container}
                  androidx.appcompat.widget.Toolbar{d2c44aa V.E...... ........ 0,0-1080,147 #7f070027 app:id/action_bar}
                    androidx.appcompat.widget.AppCompatTextView{e41389b V.ED..... ........ 42,38-382,109}
                    androidx.appcompat.widget.ActionMenuView{3e68438 V.E...... ........ 1080,0-1080,147}
                  androidx.appcompat.widget.ActionBarContextView{bf8c11 G.E...... ......I. 0,0-0,0 #7f07002f app:id/action_context_bar}
          android.view.View{4620176 V.ED..... ........ 0,1794-1080,1920 #1020030 android:id/navigationBarBackground}
          android.view.View{170c377 V.ED..... ........ 0,0-1080,63 #102002f android:id/statusBarBackground}
      Looper (main, tid 2) {d9babe4}
        (Total messages: 0, polling=false, quitting=false)
      AutofillManager:
        sessionId: 2147483647
        state: UNKNOWN
D/FragmentManager:     context: com.example.countriesdata.activities.ListActivity@cc8ca22
        client: com.example.countriesdata.activities.ListActivity@cc8ca22 (android.os.BinderProxy@dd59a96)
        enabled: false
        enabledAugmentedOnly: false
        hasService: true
        hasCallback: false
        onInvisibleCalled false
        last autofilled data: null
        id of last fill UI shown: null
        tracked views: null
        fillable ids: null
        entered ids: null
        save trigger id: null
        save on finish(): false
        options: logLvl=0, compatMode=false, augmented=false
        compat mode enabled: false
D/FragmentManager:     debug: false verbose: false
      Autofill Compat Mode: false
      No ContentCaptureManager
      ResourcesManager:
        cached apks: 0 [cache disabled]
        total apks: 1
        resources: 4
        resource impls: 3
      Local FragmentActivity cc8ca22 State:
D/FragmentManager:     mCreated=true mResumed=true mStopped=false  Active Fragments in fffd84d:
      CountryFragment{3ded6a8 (ab54e82e-6114-4343-90ec-ed8edf4125b5) id=0x7f070063}
          mFragmentId=#7f070063 mContainerId=#7f070063 mTag=null
          mState=1 mWho=ab54e82e-6114-4343-90ec-ed8edf4125b5 mBackStackNesting=1
          mAdded=true mRemoving=false mFromLayout=false mInLayout=false
          mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
          mRetainInstance=false mUserVisibleHint=true
          mFragmentManager=FragmentManager{fffd84d in HostCallbacks{c64bf02}}
          mHost=androidx.fragment.app.FragmentActivity$HostCallbacks@c64bf02
          mArguments=Bundle[{countriesList=null}]
          Child FragmentManager{6a42013 in CountryFragment{3ded6a8}}:
            FragmentManager misc state:
D/FragmentManager:           mHost=androidx.fragment.app.FragmentActivity$HostCallbacks@c64bf02
              mContainer=androidx.fragment.app.Fragment$4@759a250
              mParent=CountryFragment{3ded6a8 (ab54e82e-6114-4343-90ec-ed8edf4125b5) id=0x7f070063}
              mCurState=1 mStateSaved=false mStopped=false mDestroyed=false
      Added Fragments:
        #0: CountryFragment{3ded6a8 (ab54e82e-6114-4343-90ec-ed8edf4125b5) id=0x7f070063}
      Back Stack:
        #0: BackStackEntry{a7eac49 #0}
          mName=null mIndex=0 mCommitted=true
          Operations:
            Op #0: ADD CountryFragment{3ded6a8 (ab54e82e-6114-4343-90ec-ed8edf4125b5) id=0x7f070063}
      Back Stack Indices:
        #0: BackStackEntry{a7eac49 #0}
      FragmentManager misc state:
        mHost=androidx.fragment.app.FragmentActivity$HostCallbacks@c64bf02
        mContainer=androidx.fragment.app.FragmentActivity$HostCallbacks@c64bf02
        mCurState=4 mStateSaved=false mStopped=false mDestroyed=false
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.countriesdata, PID: 14691
    java.lang.IllegalArgumentException: No view found for id 0x7f070063 (com.example.countriesdata:id/fragments_container) for fragment CountryFragment{3ded6a8 (ab54e82e-6114-4343-90ec-ed8edf4125b5) id=0x7f070063}
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:875)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

ListActivity

public class ListActivity extends AppCompatActivity implements ListAdapter.ItemClickListener {
...
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
...
        }
    }

    @Override
    public void onItemClick(View view, int position) {

        Fragment menuFragment = new MenuFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fragments_container, menuFragment)
                .addToBackStack(null)
                .commit();
    }
}

目标片段

public class MenuFragment extends Fragment {

    int population;
    double area, gini;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_menu, container, false);

        return rootView;
    }
}

MainActivity的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/fragments_container">
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

片段的布局

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>

谢谢.

推荐答案

正如您所说的fragments_container处于MainActivity布局中,但是您正试图从ListActivity中的activity_list布局中调用container >课堂,尝试像这样更改您的setContentView

as you said your fragments_container is in MainActivity layout but you're trying to call your container from activity_list layout in your ListActivity Class , try to change your setContentView like this ;

public class ListActivity extends AppCompatActivity implements ListAdapter.ItemClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // this line

        }
    }

或尝试将fragments_container放入您的activity_list布局!

or try to put your fragments_container in your activity_list layout!

这篇关于FragmentManager:找不到ID的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆