今天突然遇到一个fragment的Bug,研究了好久~~ 还是google解决问题
E/UncaughtException-main(13661): Unable to start activity ComponentInfo{com.android.fileexplorer/com.android.fileexplorer.FileExplorerTabActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.fileexplorer.view.FileViewFragment: make sure class name exists, is public, and has an empty constructor that is publicE/UncaughtException-main(13661): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.fileexplorer/com.android.fileexplorer.FileExplorerTabActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.fileexplorer.view.FileViewFragment: make sure class name exists, is public, and has an empty constructor that is publicE/UncaughtException-main(13661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)E/UncaughtException-main(13661): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)E/UncaughtException-main(13661): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)E/UncaughtException-main(13661): at android.app.ActivityThread.access$900(ActivityThread.java:135)E/UncaughtException-main(13661): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)E/UncaughtException-main(13661): at android.os.Handler.dispatchMessage(Handler.java:102)E/UncaughtException-main(13661): at android.os.Looper.loop(Looper.java:136)E/UncaughtException-main(13661): at android.app.ActivityThread.main(ActivityThread.java:5017)E/UncaughtException-main(13661): at java.lang.reflect.Method.invokeNative(Native Method)E/UncaughtException-main(13661): at java.lang.reflect.Method.invoke(Method.java:515)E/UncaughtException-main(13661): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)E/UncaughtException-main(13661): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)E/UncaughtException-main(13661): at dalvik.system.NativeStart.main(Native Method)E/UncaughtException-main(13661): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.fileexplorer.view.FileViewFragment: make sure class name exists, is public, and has an empty constructor that is publicE/UncaughtException-main(13661): at android.app.Fragment.instantiate(Fragment.java:601)E/UncaughtException-main(13661): at android.app.FragmentState.instantiate(Fragment.java:98)E/UncaughtException-main(13661): at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1761)E/UncaughtException-main(13661): at android.app.Activity.onCreate(Activity.java:899)E/UncaughtException-main(13661): at miui.app.Activity.onCreate(Activity.java:48)E/UncaughtException-main(13661): at com.android.fileexplorer.FileExplorerTabActivity.onCreate(FileExplorerTabActivity.java:39)E/UncaughtException-main(13661): at android.app.Activity.performCreate(Activity.java:5231)E/UncaughtException-main(13661): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)E/UncaughtException-main(13661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)E/UncaughtException-main(13661): ... 12 moreE/UncaughtException-main(13661): Caused by: java.lang.InstantiationException: can't instantiate class com.android.fileexplorer.view.FileViewFragment; no empty constructorE/UncaughtException-main(13661): at java.lang.Class.newInstanceImpl(Native Method)E/UncaughtException-main(13661): at java.lang.Class.newInstance(Class.java:1208)E/UncaughtException-main(13661): at android.app.Fragment.instantiate(Fragment.java:590)E/UncaughtException-main(13661): ... 20 more解决方法:自定义的fragment最好有一个Public的参数为空的构造函数,若需要传入一个参数,可以使用下面的方法public FileViewFragment(){ } public static FileViewFragment getInstance(A a){ FileViewFragment fileViewFragment = new FileViewFragment(); fileViewFragment.a = a; return fileViewFragment; }