简体   繁体   中英

InstantiationException issue with Anonymous Class in Android

A. I want to create Popup by using AlertDialogBox Anonymous class as below :

private void showPopupChangeCurrency(final CharSequence[] items, final String title){
    ...
        myAlert = new AlertDialogBox() { //This is anonymous class

        @Override
        protected void setButtonRight() {
        ...
        }
    ...
}

B. Problem: When change orientation or language i got below InstantiationException FC issue:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loudlex/com.loudlex.setting.CloudSettings}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.loudlex.setting.CloudSettings$5: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3195)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3291)
    at android.app.ActivityThread.-wrap16(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1743)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7010)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.loudlex.setting.CloudSettings$5: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.Fragment.instantiate(Fragment.java:642)
    at android.app.FragmentState.instantiate(Fragment.java:114)
    at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1873)
    at android.app.FragmentController.restoreAllState(FragmentController.java:122)
    at android.app.Activity.onCreate(Activity.java:1010)
    at com.loudlex.utility.ConfWindow.onCreate(ConfWindow.java:60)
    at com.loudlex.setting.CloudSettings.onCreate(CloudSettings.java:105)
    at android.app.Activity.performCreate(Activity.java:6786)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3148)

C. Question: I want to know is there any solution to work fine with anonymous class.

Note . I known there are some method to overcome this issue, ex: by using public static nested class or separate to top level class. Thanks

You have three solutions:

  1. Move CloudSettings in a separate class
  2. Change your inner class to public static

     public static class CloudSettings extends DialogFragment
  3. Calling myAlert.setRetainInstance(true);

    Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change)

    Reference: http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM