我目前在我的应用上遇到崩溃,我只能通过Crashlytics 检测到这个堆栈:
java.lang.RuntimeException: Unable to resume activity {com.my.app/com.my.app.activities.SplashActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080048 (com.my.app.activities:id/container) for fragment l{453a70a8 #0 id=0x7f080048 FragmentXpto}
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2836)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2865)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2291)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080048 (com.my.app:id/container) for fragment l{453a70a8 #0 id=0x7f080048 DropboxFirstLinkFragment}
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:939)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1126)
at android.support.v4.app.BackStackRecord.run(SourceFile:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1489)
at android.support.v4.app.FragmentActivity.onPostResume(SourceFile:434)
at android.support.v7.app.ActionBarActivity.onPostResume(SourceFile:115)
at android.app.Activity.performResume(Activity.java:5323)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2826)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2865)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2291)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(NativeStart.java)
我的活动布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></FrameLayout>
我检查了Android源代码,这是生成源代码的抛出:
void moveToState(Fragment f, int newState, int transit, int transitionStyle,
boolean keepActive) {
// Fragments that are not currently added will sit in the onCreate() state.
...
case Fragment.CREATED:
if (newState > Fragment.CREATED) {
if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
if (!f.mFromLayout) {
ViewGroup container = null;
if (f.mContainerId != 0) {
container = (ViewGroup)mContainer.findViewById(f.mContainerId);
if (container == null && !f.mRestored) {
throwException(new IllegalArgumentException(
"No view found for id 0x"
+ Integer.toHexString(f.mContainerId) + " ("
+ f.getResources().getResourceName(f.mContainerId)
+ ") for fragment " + f));
}
}
...
这是一个只能“一生一次”再现的问题...所以我没有传递错误的视图ID,或类似的东西,或者它会经常崩溃。
更多信息:
我使用FragmentTransaction替换SplashActivity AFTER setContentView或onResume的onCreate方法:
FragmentTransaction transaction = mFragmentManager.beginTransaction()。replace(R.id.container, newFragment,tag); 器transaction.commit();
有人可以解释一下为什么/何时会发生这种情况,我该如何预防呢?
答案 0 :(得分:2)
我已设法通过验证 if findViewById(R.id.container) != null
并在此案例中仅执行Fragment.replace来避免此问题。
只要它为null,我就存储操作,并在活动的下一个onResume上重试。
我想我已经发现了这个问题,因为我看到碎片传递onResume而没有在它们被恢复时传递onCreateView,不在FragmentManager堆栈的顶部并且活动在后台被杀死之后。