当我尝试从后台恢复我的应用程序时,由于片段启动的不规则序列而崩溃。但是,如果应用程序从头开始运行,一切都还可以。碎片结构和日志请见下文。
我目前正在使用常规片段,但支持片段的情况是一样的。 API 27。
<activity android:name=".ActivityMain"
android:theme="@style/AppTheme"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize">
碎片结构:
MainActivity
- FragmentProfileBase
- FragmentProfile (child fragment)
设置基本片段功能:
private void setFragment(@NonNull Fragment fragment, @Nullable Bundle bundle){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
fragment.setArguments(bundle);
if (fragmentCurrent != null) {
String classCurrent = fragmentCurrent.getClass().getName();
String classFragment = fragment.getClass().getName();
if (classCurrent.equalsIgnoreCase(classFragment)) {
FragmentBaseAbstract fragmentBaseAbstract = (FragmentBaseAbstract) fragmentCurrent;
fragmentBaseAbstract.setTabClicked();
return;
}
transaction.hide(fragmentCurrent);
}
if (fragment.isAdded())
transaction.show(fragment);
else
transaction.add(R.id.frameContainer, fragment);
transaction.commit();
getFragmentManager().executePendingTransactions();
fragmentCurrent = fragment;
}
设置子片段功能:
public void openFragment(Fragment self, Fragment fragment, boolean removeSelf){
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (self != null) {
if (removeSelf)
transaction.remove(self);
transaction.addToBackStack(self.getClass().getName());
}
transaction
.add(getBaseContainer(), fragment)
.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
常规跑步记录:
D/App: onCreate
D/ActivityMain: onCreate
D/FragmentProfileBase: onCreate
D/FragmentProfileBase: onCreateView
D/FragmentProfile: onCreate
D/FragmentProfile: onCreateView
D/FragmentProfile: onCreateOptionsMenu
从后台运行日志:
D/App: onCreate
D/FragmentProfile: onCreate actionBar = null
D/FragmentProfileBase: onCreate
D/ActivityMain: onCreate
D/FragmentProfileBase: onCreate
D/FragmentProfileBase: onCreateView
D/FragmentProfile: onCreateView
D/FragmentProfile: onCreate actionBar = android.support.v7.app.ToolbarActionBar@8f9c1fc
D/FragmentProfile: onCreateView
D/FragmentProfileBase: onCreateView
D/FragmentProfile: onCreate actionBar = android.support.v7.app.ToolbarActionBar@8f9c1fc
D/FragmentProfile: onCreateView
完整日志:
02-24 21:21:53.875 9592-9592/? I/zygote64: Late-enabling -Xcheck:jni
02-24 21:21:54.815 9592-9592/com.homework.personalstuff I/MultiDex: VM with version 2.1.0 has multidex support
02-24 21:21:54.815 9592-9592/com.homework.personalstuff I/MultiDex: Installing application
02-24 21:21:54.815 9592-9592/com.homework.personalstuff I/MultiDex: VM has multidex support, MultiDex support library is disabled.
02-24 21:21:54.880 9592-9592/com.homework.personalstuff W/zygote64: Unsupported class loader
02-24 21:21:54.908 9592-9592/com.homework.personalstuff W/zygote64: Skipping duplicate class check due to unsupported classloader
02-24 21:21:54.925 9592-9592/com.homework.personalstuff W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
02-24 21:21:54.931 9592-9592/com.homework.personalstuff W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
02-24 21:21:54.952 9592-9592/com.homework.personalstuff I/BiChannelGoogleApi: [FirebaseAuth: ] No Fallback module; NOT setting up for lazy initialization
02-24 21:21:54.978 9592-9623/com.homework.personalstuff W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
02-24 21:21:54.989 9592-9623/com.homework.personalstuff I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
02-24 21:21:54.989 9592-9623/com.homework.personalstuff I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
02-24 21:21:55.007 9592-9597/com.homework.personalstuff I/zygote64: Do partial code cache collection, code=30KB, data=20KB
02-24 21:21:55.007 9592-9597/com.homework.personalstuff I/zygote64: After code cache collection, code=30KB, data=20KB
02-24 21:21:55.007 9592-9597/com.homework.personalstuff I/zygote64: Increasing code cache capacity to 128KB
02-24 21:21:55.017 9592-9592/com.homework.personalstuff D/FirebaseAuth: Notifying id token listeners about user ( uH6Bo1NMUOVOIQNiB0pLQswb78l1 ).
02-24 21:21:55.053 9592-9592/com.homework.personalstuff D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
02-24 21:21:55.139 9592-9592/com.homework.personalstuff V/FA: Registered activity lifecycle callback
02-24 21:21:55.141 9592-9592/com.homework.personalstuff I/FirebaseInitProvider: FirebaseApp initialization successful
02-24 21:21:55.143 9592-9592/com.homework.personalstuff I/InstantRun: starting instant run server: is main process
02-24 21:21:55.166 9592-9627/com.homework.personalstuff V/FA: Collection enabled
02-24 21:21:55.166 9592-9627/com.homework.personalstuff V/FA: App package, google app id: com.homework.personalstuff, 1:633452378895:android:e2b301a85dc1fba2
02-24 21:21:55.168 9592-9627/com.homework.personalstuff I/FA: App measurement is starting up, version: 11910
02-24 21:21:55.168 9592-9627/com.homework.personalstuff I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
02-24 21:21:55.168 9592-9627/com.homework.personalstuff I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.homework.personalstuff
02-24 21:21:55.168 9592-9627/com.homework.personalstuff D/FA: Debug-level message logging enabled
02-24 21:21:55.180 9592-9592/com.homework.personalstuff D/App: onCreate
02-24 21:21:55.197 9592-9592/com.homework.personalstuff D/ImageCache: Memory cache created (size = 58982)
02-24 21:21:55.298 9592-9592/com.homework.personalstuff D/ImageCache: Disk cache initialized
02-24 21:21:55.326 9592-9592/com.homework.personalstuff D/UserProfileManager: UserProfileManager
02-24 21:21:55.335 9592-9631/com.homework.personalstuff D/NetworkSecurityConfig: No Network Security Config specified, using platform default
02-24 21:21:55.383 9592-9627/com.homework.personalstuff V/FA: Connecting to remote service
02-24 21:21:55.405 9592-9627/com.homework.personalstuff V/FA: Connection attempt already in progress
02-24 21:21:55.407 9592-9640/com.homework.personalstuff D/Malevich: ImageWorker: HTTP cache initialized
02-24 21:21:55.456 9592-9597/com.homework.personalstuff I/zygote64: Do partial code cache collection, code=61KB, data=43KB
02-24 21:21:55.457 9592-9597/com.homework.personalstuff I/zygote64: After code cache collection, code=58KB, data=42KB
02-24 21:21:55.457 9592-9597/com.homework.personalstuff I/zygote64: Increasing code cache capacity to 256KB
02-24 21:21:55.487 9592-9592/com.homework.personalstuff V/FA: onActivityCreated
02-24 21:21:55.591 9592-9592/com.homework.personalstuff D/FragmentProfile: onCreate
02-24 21:21:55.622 9592-9592/com.homework.personalstuff D/FragmentProfileBase: onCreate
02-24 21:21:55.622 9592-9592/com.homework.personalstuff D/ActivityMain: onCreate
02-24 21:21:55.827 9592-9592/com.homework.personalstuff D/UserProfileManager: checkSignIn
02-24 21:21:55.913 9592-9592/com.homework.personalstuff D/UserProfileManager: checkSignIn prefs facebookIsSignedIn = true
02-24 21:21:55.913 9592-9592/com.homework.personalstuff D/UserProfileManager: checkSignIn firebase getCurrentUser = com.google.firebase.auth.internal.zzh@df479cd
02-24 21:21:55.914 9592-9592/com.homework.personalstuff D/UserProfileManager: dbInitials
02-24 21:21:55.914 9592-9592/com.homework.personalstuff D/UserProfileManager: setDbLastVisited
02-24 21:21:55.954 9592-9592/com.homework.personalstuff D/UserProfileManager: subscribeFriends
02-24 21:21:55.956 9592-9664/com.homework.personalstuff I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
02-24 21:21:55.957 9592-9664/com.homework.personalstuff I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
02-24 21:21:55.959 9592-9592/com.homework.personalstuff D/UserProfileManager: subscribeProfileChanged
02-24 21:21:55.962 9592-9592/com.homework.personalstuff D/ActivityMain: checkSignIn = true
02-24 21:21:55.969 9592-9592/com.homework.personalstuff D/StuffManager: Create
02-24 21:21:56.018 9592-9664/com.homework.personalstuff W/zygote64: Unsupported class loader
02-24 21:21:56.021 9592-9592/com.homework.personalstuff D/StuffManager: updatePresets
02-24 21:21:56.042 9592-9592/com.homework.personalstuff D/StuffManager: updatePresets listParams = []
02-24 21:21:56.051 9592-9592/com.homework.personalstuff D/SocialManager: Create
02-24 21:21:56.051 9592-9592/com.homework.personalstuff D/ActivityMain: hasExtra type = null
02-24 21:21:56.051 9592-9592/com.homework.personalstuff D/ActivityMain: setFragment
02-24 21:21:56.058 9592-9592/com.homework.personalstuff D/FragmentProfileBase: onCreateView
02-24 21:21:56.060 9592-9592/com.homework.personalstuff D/FragmentProfileBase: openFragment removeSelf true
02-24 21:21:56.062 9592-9592/com.homework.personalstuff D/FragmentProfile: onCreateView
02-24 21:21:56.370 9592-9597/com.homework.personalstuff I/zygote64: Do full code cache collection, code=118KB, data=108KB
02-24 21:21:56.370 9592-9597/com.homework.personalstuff I/zygote64: After code cache collection, code=110KB, data=79KB
02-24 21:21:56.377 9592-9592/com.homework.personalstuff D/FragmentProfile: onCreate
02-24 21:21:56.380 9592-9592/com.homework.personalstuff D/FragmentProfile: onCreateView
02-24 21:21:56.449 9592-9592/com.homework.personalstuff D/FragmentProfileBase: onCreate
02-24 21:21:56.450 9592-9592/com.homework.personalstuff D/FragmentProfileBase: onCreateView
02-24 21:21:56.452 9592-9592/com.homework.personalstuff D/FragmentProfileBase: openFragment removeSelf true
02-24 21:21:56.453 9592-9592/com.homework.personalstuff D/FragmentProfile: onCreate
02-24 21:21:56.454 9592-9592/com.homework.personalstuff D/FragmentProfile: onCreateView
02-24 21:21:56.520 9592-9592/com.homework.personalstuff D/FragmentProfileBase: onStart
02-24 21:21:56.520 9592-9592/com.homework.personalstuff D/FragmentProfile: onStart
02-24 21:21:56.521 9592-9592/com.homework.personalstuff D/AndroidRuntime: Shutting down VM
02-24 21:21:56.525 9592-9592/com.homework.personalstuff E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.homework.personalstuff, PID: 9592
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.homework.personalstuff/com.homework.personalstuff.ActivityMain}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.homework.personalstuff.FragmentProfilePresenter.bindView(com.homework.personalstuff.FragmentProfilePresenter$IFragmentView)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.homework.personalstuff.FragmentProfilePresenter.bindView(com.homework.personalstuff.FragmentProfilePresenter$IFragmentView)' on a null object reference
at com.homework.personalstuff.FragmentProfile.onStart(FragmentProfile.java:133)
at android.support.v4.app.Fragment.performStart(Fragment.java:2287)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1458)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3227)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3186)
at android.support.v4.app.Fragment.performStart(Fragment.java:2293)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1458)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3227)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3186)
at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:203)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:582)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at com.homework.personalstuff.ActivityMain.onStart(ActivityMain.java:80)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at android.app.Activity.performStart(Activity.java:7019)
答案 0 :(得分:2)
正如我从您的日志中看到的那样,您正在实现MVP架构,因此您需要在使用之前将片段的实例注入到片段中。如果您手动设置演示者onAttachFragment
回调活动或片段(取决于您的片段的托管位置)是一个很好的地方。如果使用依赖注入onViewCreated
生命周期回调适合注入。
答案 1 :(得分:0)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.homework.personalstuff.FragmentProfilePresenter.bindView(com.homework.personalstuff.FragmentProfilePresenter$IFragmentView)' on a null object reference
at com.homework.personalstuff.FragmentProfile.onStart(FragmentProfile.java:133)
从上面的日志中看,FragmentProfile.java
在第133行有一些问题 ,可能是一个方法正在调用null对象引用。
请在代码中进行空检查!