修改 我意识到问题没有解决,所以我添加了堆栈跟踪。
从活动进行片段事务期间,会发生此错误。我阅读了先前问题的解决方案,但找不到相关案例。事务是通过适配器的回调方法完成的。我是第一次这样做,所以也许有些地方不在。我可以添加相关的回调代码。
最终堆栈跟踪(漆成红色的部分)
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>
谢谢。
答案 0 :(得分:1)
正如您所说的fragments_container
处于MainActivity
布局,但是您尝试从container
类中的activity_list
布局调用ListActivity
,请尝试像这样更改您的setContentView
;
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
布局中!
答案 1 :(得分:0)
尝试一下
public class ListActivity extends AppCompatActivity implements ListAdapter.ItemClickListener {
FragmentManager fragmentManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
fragmentManager = getSupportFragmentManager();
}
}
@Override
public void onItemClick(View view, int position) {
Fragment menuFragment = new MenuFragment();
FragmentTransaction ft = fragmentManager.beginTransaction(); // FragmentManager
ft.add(R.id.fragments_container, menuFragment)
.addToBackStack(null).commit();
}
}
答案 2 :(得分:0)
公共类ListActivity扩展了AppCompatActivity实现的ListAdapter.ItemClickListener {@Override protected void onCreate(@Nullable Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //此行}} 请致电activity_main代替activity_list
答案 3 :(得分:0)
与NavigationComponent
一起使用时,视图寻呼机也存在类似问题。问题是,我扩展了FragmentPagerAdapter
并使用childFragmentManger
对象来创建ViewPager适配器。
我已将FragmentPagerAdapter
替换为FragmentStatePagerAdapter
,并使用了supportFragmentManager
。
我解决了问题.. !!