Unable to instantiate fragment make sure class nam

今天忽然遇到一個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 public
E/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 public
E/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 public
E/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 more
E/UncaughtException-main(13661): Caused by: java.lang.InstantiationException: can't instantiate class com.android.fileexplorer.view.FileViewFragment; no empty constructor
E/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;
    }java