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)".

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
}

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".

Here's the log:

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.

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
   }
}

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): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ambious.androidtroper.MainActivity$ArticleFragment: Ensure the following:

  • Class name exists
  • Class is public
  • Class is static
  • Empty public constructor exists

try this, making it static class:

public static class ArticleFragment extends Fragment  {
   public ArticleFragment(){
      //Empty constructor
   }
}

其他提示

A couple of things to check:

You should either remove the constructor on ArticleFragment, or be sure to call super() within your constructor.

Secondly, you might have to declare ArticleFragment as a static nested class within your activity. I typically write fragments as separate classes rather than as inner classes so I'm unsure of the ramifications to the android lifecycle when you declare them as you have..

activity life cycle

Process is killed and your data will be clear but when you resume app then method onCreate will be call and the state previously will try to restore. However, your app cannot restore. Because everything was cleared. the classes has been unloaded out of memory when other app need more memory.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top