Super.onCreate (savedInstanceState) crash on resuming activity

My app activity starts fine, works fine, everything is dandy - but if you switch to another app and the app gets destroyed by the garbage collector, then when you return to the app, it will be broken into "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 simple. Except when I switch using the taskwitcher and then switch back after the application has been destroyed by the garbagecollector to free memory, the application 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, has a public empty constructor, and doesn't throw the same error on normal application startup:

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

      

I'm really at a loss. The error is triggered in "super.onCreate ()", which I cannot omit (obviously), and is only triggered when the application is re-created after garbage collection. What am I missing? Is there anything else I might have forgotten to mention? Thank.

+1


source to share


3 answers


E / AndroidRuntime (25512): Caused: android.support.v4.app.Fragment $ InstantiationException: Failed to instantiate ambious.androidtroper.MainActivity $ ArticleFragment: provide the following:

  • Class name exists
  • The class is public.
  • Class static
  • An empty public constructor exists


try this by making it a static class:

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

      

+3


source


A few things to check:

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



Second, you may need to declare ArticleFragment as a static nested class in your activity. I usually write fragments as separate classes, not as inner classes, so I'm not sure about the branches of the Android lifecycle when you declare them as they are.

0


source


activity life cycle

The process is killed and your data is clear, but when you resume your application then the onCreate method will be called and the state will previously try to restore. However, your application cannot recover. Because everything has been cleared. classes have been unloaded from memory when another application needs more memory.

0


source







All Articles