答:我想用AlertDialogBox匿名類,如下創建彈出窗口:InstantiationException問題與匿名類
private void showPopupChangeCurrency(final CharSequence[] items, final String title){
...
myAlert = new AlertDialogBox() { //This is anonymous class
@Override
protected void setButtonRight() {
...
}
...
}
B.問題:當改變方向或語言,我下面有InstantiationException FC問題:
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.問: 我想知道是否有任何解決方案可以很好地與匿名類一起工作。
注意。我知道有一些方法可以解決這個問題,例如:通過使用公共靜態嵌套類或獨立於頂級類。 感謝
「確保類名存在,是公共的,和有一個**空的構造函數public **「。你的自定義類有一個空的構造函數嗎? – Sufian
我認爲答案是NO,因爲CloudSettings $ 5是一個匿名類,所以它沒有顯式聲明構造函數,我認爲編譯器也不會自動爲Anonymous類添加空構造函數。 – NguyenDat
我認爲你應該分享代碼。我們現在就像做猜測工作。 :) – Sufian