我的应用程序有一个对话框和两个活动,风景和肖像。
当我在其中一个活动中并且在对话框打开时旋转屏幕时,应用程序崩溃。
为了不发生这种情况,我该怎么处理?
这是logcat输出:(空格表示事物开始向南的地方)
11-15 15:56:25.322: D/AndroidRuntime(956): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-15 15:56:25.322: D/AndroidRuntime(956): CheckJNI is ON
11-15 15:56:26.342: W/ThrottleService(77): unable to find stats for iface rmnet0
11-15 15:56:26.382: D/AndroidRuntime(956): Calling main entry com.android.commands.pm.Pm
11-15 15:56:26.442: D/AndroidRuntime(956): Shutting down VM
11-15 15:56:26.452: D/dalvikvm(956): GC_CONCURRENT freed 101K, 78% free 463K/2048K, paused 1ms+1ms
11-15 15:56:26.462: D/dalvikvm(956): Debugger has detached; object registry had 1 entries
11-15 15:56:26.472: I/AndroidRuntime(956): NOTE: attach of thread 'Binder Thread #3' failed
11-15 15:56:27.112: D/AndroidRuntime(969): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-15 15:56:27.112: D/AndroidRuntime(969): CheckJNI is ON
11-15 15:56:28.002: D/AndroidRuntime(969): Calling main entry com.android.commands.am.Am
11-15 15:56:28.032: I/ActivityManager(77): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.soft.test/.myActivity} from pid 969
11-15 15:56:28.032: W/WindowManager(77): Failure taking screenshot for (130x178) to layer 21005
11-15 15:56:28.092: D/AndroidRuntime(969): Shutting down VM
11-15 15:56:28.142: D/dalvikvm(980): Not late-enabling CheckJNI (already on)
11-15 15:56:28.163: D/dalvikvm(969): GC_CONCURRENT freed 102K, 77% free 485K/2048K, paused 1ms+1ms
11-15 15:56:28.163: D/dalvikvm(969): Debugger has detached; object registry had 1 entries
11-15 15:56:28.172: I/AndroidRuntime(969): NOTE: attach of thread 'Binder Thread #3' failed
11-15 15:56:28.252: I/ActivityManager(77): Start proc com.soft.test for activity com.soft.test/.myActivity: pid=980 uid=10046 gids={3003}
11-15 15:56:28.362: I/dalvikvm(980): Turning on JNI app bug workarounds for target SDK version 8...
11-15 15:56:28.452: W/NetworkManagementSocketTagger(77): setKernelCountSet(10046, 1) failed with errno -2
11-15 15:56:29.652: D/dalvikvm(980): GC_CONCURRENT freed 222K, 5% free 6723K/7047K, paused 5ms+16ms
11-15 15:56:29.873: V/PhoneStatusBar(141): setLightsOn(true)
11-15 15:56:29.953: D/gralloc_goldfish(980): Emulator without GPU emulation detected.
11-15 15:56:30.303: I/ActivityManager(77): Displayed com.soft.test/.myActivity: +2s234ms
11-15 15:56:38.073: W/ActivityManager(77): Launch timeout has expired, giving up wake lock!
11-15 15:56:38.510: W/ActivityManager(77): Activity idle timeout for ActivityRecord{4119aad0 com.soft.test/.myActivity}
11-15 15:56:38.563: W/NetworkManagementSocketTagger(77): setKernelCountSet(10009, 0) failed with errno -2
11-15 15:56:54.973: D/dalvikvm(980): GC_CONCURRENT freed 93K, 3% free 7095K/7303K, paused 7ms+27ms
11-15 15:57:00.052: I/InputReader(77): Reconfiguring input devices. changes=0x00000004
11-15 15:57:00.052: I/InputReader(77): Device reconfigured: id=0, name='qwerty2', surface size is now 320x480, mode is 1
11-15 15:57:00.052: W/SurfaceFlinger(35): createScreenshotSurface failed (Function not implemented)
11-15 15:57:00.062: I/ActivityManager(77): Config changed: {1.0 310mcc260mnc en_US layoutdir=0 sw320dp w320dp h407dp smll port finger -keyb/v/h tball/v s.14}
11-15 15:57:01.072: D/dalvikvm(980): newInstance failed: no <init>()
11-15 15:57:01.072: D/AndroidRuntime(980): Shutting down VM
11-15 15:57:01.082: W/dalvikvm(980): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-15 15:57:01.122: E/AndroidRuntime(980): FATAL EXCEPTION: main
11-15 15:57:01.122: E/AndroidRuntime(980): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soft.test/com.soft.test.myActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.soft.test.HistoryDialogFragment: make sure class name exists, is public, and has an empty constructor that is public
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.access$700(ActivityThread.java:123)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.os.Looper.loop(Looper.java:137)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 15:57:01.122: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-15 15:57:01.122: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-15 15:57:01.122: E/AndroidRuntime(980): at dalvik.system.NativeStart.main(Native Method)
11-15 15:57:01.122: E/AndroidRuntime(980): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.soft.test.HistoryDialogFragment: make sure class name exists, is public, and has an empty constructor that is public
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
11-15 15:57:01.122: E/AndroidRuntime(980): at com.soft.test.myActivity.onCreate(myActivity.java:66)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.Activity.performCreate(Activity.java:4465)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-15 15:57:01.122: E/AndroidRuntime(980): ... 12 more
11-15 15:57:01.122: E/AndroidRuntime(980): Caused by: java.lang.InstantiationException: can't instantiate class com.soft.test.HistoryDialogFragment; no empty constructor
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.Class.newInstanceImpl(Native Method)
11-15 15:57:01.122: E/AndroidRuntime(980): at java.lang.Class.newInstance(Class.java:1319)
11-15 15:57:01.122: E/AndroidRuntime(980): at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
11-15 15:57:01.122: E/AndroidRuntime(980): ... 19 more
11-15 15:57:01.192: W/ActivityManager(77): Force finishing activity com.soft.test/.myActivity
11-15 15:57:01.312: I/ARMAssembler(35): generated scanline__00000077:03545404_00008500_00000000 [ 29 ipp] (43 ins) at [0x41357cd8:0x41357d84] in 5238733 ns
11-15 15:57:01.322: W/WindowManager(77): Failure taking screenshot for (120x162) to layer 21010
11-15 15:57:01.832: W/ActivityManager(77): Activity pause timeout for ActivityRecord{4119aad0 com.soft.test/.myActivity}
11-15 15:57:01.932: W/NetworkManagementSocketTagger(77): setKernelCountSet(10009, 1) failed with errno -2
11-15 15:57:02.152: D/dalvikvm(141): GC_CONCURRENT freed 376K, 21% free 7320K/9159K, paused 4ms+121ms
11-15 15:57:02.282: D/dalvikvm(183): GC_EXPLICIT freed 201K, 8% free 7501K/8071K, paused 22ms+14ms
11-15 15:57:02.292: E/StrictMode(183): class com.android.launcher2.Launcher; instances=2; limit=1
11-15 15:57:02.292: E/StrictMode(183): android.os.StrictMode$InstanceCountViolation: class com.android.launcher2.Launcher; instances=2; limit=1
11-15 15:57:02.292: E/StrictMode(183): at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
11-15 15:57:03.432: W/NetworkManagementSocketTagger(77): setKernelCountSet(10046, 0) failed with errno -2
11-15 15:57:03.792: D/dalvikvm(209): GC_CONCURRENT freed 155K, 4% free 6694K/6919K, paused 4ms+3ms
11-15 15:57:04.933: I/Process(980): Sending signal. PID: 980 SIG: 9
11-15 15:57:05.273: I/ActivityManager(77): Process com.soft.test (pid 980) has died.
11-15 15:57:05.273: I/WindowManager(77): WIN DEATH: Window{4122bc60 com.soft.test/com.soft.test.myActivity paused=false}
11-15 15:57:05.513: D/dalvikvm(77): GC_EXPLICIT freed 237K, 11% free 8967K/10055K, paused 6ms+11ms
11-15 15:57:13.477: W/ActivityManager(77): Activity destroy timeout for ActivityRecord{4119aad0 com.soft.test/.myActivity}
答案 0 :(得分:7)
当手机旋转时,活动总会被破坏并重新创建。这意味着它还将尝试重新创建片段。由于您的Fragment缺少空构造函数,因此无法重新创建,因此错误:“com.soft.test.HistoryDialogFragment; no empty constructor
”。
您还可以在片段onCreate()中使用“setRetainInstance(true)
”来使其不被杀死,但只是暂时删除(与活动分离)。
这是一个例子: http://android-er.blogspot.se/2012/08/retain-fragment-instance-across.html
在这里你可以阅读更多: https://stackoverflow.com/a/11318942/1068167
答案 1 :(得分:1)
如果你可以在屏幕旋转时关闭DialogFragment
,这是另一种可能的解决方案。保留一个实例变量current_dialog
,用于跟踪当前打开的DialogFragment
,然后将此代码添加到onPause()
:
if (current_dialog != null)
current_dialog.dismiss();
答案 2 :(得分:-1)
尝试在AndroidManifest.xml中为您的活动添加android:configChanges =“orientation”