recreate导致的Fragment报的错误

在做夜间模式的时候在Fragment中调用了recreate()然后就报了这个错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.xxxx.test, PID: 2790
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.test/com.xxxx.test.SettingsActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.xxxx.test.SettingsActivity$SettingsFragment:
 make sure class name exists, is public, and has an empty constructor that is public
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
                      at android.app.ActivityThread.access$900(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.xxxx.test.SettingsActivity$SettingsFragment: make sure class name exists, is public, and has an empty constructor that is public
                      at android.app.Fragment.instantiate(Fragment.java:622)
                      at android.app.FragmentState.instantiate(Fragment.java:104)
                      at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1775)
                      at android.app.Activity.onCreate(Activity.java:933)
                      at android.support.v4.app.BaseFragmentActivityDonut.onCreate(BaseFragmentActivityDonut.java:41)
                      at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:315)
                      at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
                      at com.xxxx.test.SettingsActivity.onCreate(SettingsActivity.java:41)
                      at android.app.Activity.performCreate(Activity.java:5990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                   Caused by: java.lang.InstantiationException: class com.xxxx.test.SettingsActivity$SettingsFragment has no zero argument constructor
                      at java.lang.Class.newInstance(Class.java:1597)
                      at android.app.Fragment.instantiate(Fragment.java:611)
                      at android.app.FragmentState.instantiate(Fragment.java:104) 
                      at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1775) 
                      at android.app.Activity.onCreate(Activity.java:933) 
                      at android.support.v4.app.BaseFragmentActivityDonut.onCreate(BaseFragmentActivityDonut.java:41) 
                      at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:315) 
                      at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85) 
                      at com.cheng.waste.SettingsActivity.onCreate(SettingsActivity.java:41) 
                      at android.app.Activity.performCreate(Activity.java:5990) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                   Caused by: java.lang.NoSuchMethodException:  []
                      at java.lang.Class.getConstructor(Class.java:531)
                      at java.lang.Class.getDeclaredConstructor(Class.java:510)
                      at java.lang.Class.newInstance(Class.java:1595)
                      at android.app.Fragment.instantiate(Fragment.java:611) 
                      at android.app.FragmentState.instantiate(Fragment.java:104) 
                      at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1775) 
                      at android.app.Activity.onCreate(Activity.java:933) 
                      at android.support.v4.app.BaseFragmentActivityDonut.onCreate(BaseFragmentActivityDonut.java:41) 
                      at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:315) 
                      at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85) 
                      at com.cheng.waste.SettingsActivity.onCreate(SettingsActivity.java:41) 
                      at android.app.Activity.performCreate(Activity.java:5990) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

看报错内容,应该这个Fragment没有public的构造函数,但是我加了之后,还是会报错,查了好久发现,Fragment不要弄成内部类,移到外面,问题解决

你可能感兴趣的:(recreate导致的Fragment报的错误)