活动恢复时super.onCreate(savedInstanceState)崩溃 [英] super.onCreate(savedInstanceState) crashes when activity resumes
问题描述
我的应用程序的主要活动开始正常,运行良好,一切正常.但是,如果您切换到另一个应用程序,并且该应用程序被垃圾收集器破坏,则当您移回该应用程序时,它将崩溃为超级". onCreate(savedInstanceState)".
My app's main activity starts fine, works fine, everything is dandy - but if you switch away to another app and the app is destroyed by the garbage collector, when you move back to the app it'll crash on "super.onCreate(savedInstanceState)".
这是我的onCreate():
This is my onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(LOG_TAG,"Activity Created");
super.onCreate(savedInstanceState);
... //Other stuff that isn't relevent
}
非常简单. 除非我使用taskswitcher离开,然后在应用被垃圾收集器杀死后释放回去以释放内存,否则一旦"super.OnCreate(savedInstanceState)被调用",应用就会崩溃.
Pretty straightforward. Except when I switch away using taskswitcher and then switch back after the app is killed by the garbagecollector to free memory, the app crashes as soon as "super.OnCreate(savedInstanceState) is called".
这是日志:
I/ActivityManager( 629): Start proc ambious.androidtroper for activity ambious.androidtroper/.MainActivity: pid=25512 uid=10016 gids={50016, 3003, 1028}
D/AndroidTroper(25512): Activity Created
E/AndroidRuntime(25512): FATAL EXCEPTION: main
E/AndroidRuntime(25512): java.lang.RuntimeException: Unable to start activity ComponentInfo{ambious.androidtroper/ambious.androidtroper.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ambious.androidtroper.MainActivity$ArticleFragment: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(25512): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2350)
E/AndroidRuntime(25512): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2400)
E/AndroidRuntime(25512): at android.app.ActivityThread.access$600(ActivityThread.java:153)
E/AndroidRuntime(25512): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
E/AndroidRuntime(25512): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(25512): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(25512): at android.app.ActivityThread.main(ActivityThread.java:5295)
E/AndroidRuntime(25512): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25512): at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(25512): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
E/AndroidRuntime(25512): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
E/AndroidRuntime(25512): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25512): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ambious.androidtroper.MainActivity$ArticleFragment: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(25512): at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
E/AndroidRuntime(25512): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
E/AndroidRuntime(25512): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1783)
E/AndroidRuntime(25512): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
E/AndroidRuntime(25512): at ambious.androidtroper.MainActivity.onCreate(MainActivity.java:88)
E/AndroidRuntime(25512): at android.app.Activity.performCreate(Activity.java:5271)
E/AndroidRuntime(25512): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(25512): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
E/AndroidRuntime(25512): ... 11 more
E/AndroidRuntime(25512): Caused by: java.lang.InstantiationException: can't instantiate class ambious.androidtroper.MainActivity$ArticleFragment; no empty constructor
E/AndroidRuntime(25512): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(25512): at java.lang.Class.newInstance(Class.java:1130)
E/AndroidRuntime(25512): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
E/AndroidRuntime(25512): ... 18 more
W/ActivityManager( 629): Force finishing activity ambious.androidtroper/.MainActivity
I/ActivityManager( 629): Process ambious.androidtroper (pid 25512) has died.
现在这很奇怪,因为错误(ArticleFragment)中指定的类是公共的,所以DOES具有公共的空构造函数,并且在常规启动应用程序时不会引起相同的错误:
Now this is weird, because the class specified in the error (ArticleFragment) IS public, DOES have a public empty constructor and doesn't cause the same error on a regular launch of the app:
public class ArticleFragment extends Fragment {
public ArticleFragment(){
//Empty constructor
}
}
我真的很茫然.该错误是在"super.onCreate()"处触发的,我无法忽略(很明显),并且仅在垃圾回收后重新创建该应用程序时才触发该错误. 我想念什么?还有其他我可能忘记提及的相关内容吗? 谢谢.
I'm really at a loss here. The error is triggered at "super.onCreate()" which I can not omit (obviously), and is only triggered when the app is re-created after being garbage-collected. What am I missing? Is there anything else relevant that I may have forgotten to mention? Thank you.
推荐答案
E/AndroidRuntime(25512):原因: android.support.v4.app.Fragment $ InstantiationException:无法实例化片段ambious.androidtroper.MainActivity $ ArticleFragment:确保满足以下条件:
E/AndroidRuntime(25512): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ambious.androidtroper.MainActivity$ArticleFragment: Ensure the following:
- 类名存在
- 班级是公开的
- 类是静态的
- 存在空公共构造函数
尝试一下,使其成为静态类:
try this, making it static class:
public static class ArticleFragment extends Fragment {
public ArticleFragment(){
//Empty constructor
}
}
这篇关于活动恢复时super.onCreate(savedInstanceState)崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!