android – 当DialogFragment在顶部时,方向更改时的InstantiationException

我的Fragment类中有一个DialogFragment被定义为内部类.即使出现以下异常,方向更改:
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment my.package.fragments.ImportFragment$FailedImportDialog: make sure class name exists,is public,and has an empty constructor that is public
        at android.app.Fragment.instantiate(Fragment.java:585)
        at android.app.FragmentState.instantiate(Fragment.java:96)
        at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1682)
        at android.app.Activity.onCreate(Activity.java:861)
        at my.package.activities.ImportActivity.onCreate(ImportActivity.java:8)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:1920)
        ... 12 more
        Caused by: java.lang.InstantiationException: can't instantiate class my.package.fragments.ImportFragment$FailedImportDialog; no empty constructor
        at java.lang.class.newInstanceImpl(Native Method)
        at java.lang.class.newInstance(Class.java:1319)
        at android.app.Fragment.instantiate(Fragment.java:574)

我有公共建设者:

class FailedImportDialog extends DialogFragment {

        private EditText edtPassword;
        private Button button;

        public FailedImportDialog() { // Here it is!
        }

        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.another_password_dialog,container,false);
            edtPassword = (EditText) v.findViewById(R.id.another_password_dialog_et_password);

            getDialog().setTitle(R.string.Failed_to_decrypt);

            Button button = (Button) v.findViewById(R.id.another_password_dialog_btn_ok);
            button.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

            });

            return v;

    }
}

这是xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:padding="10dp">

    <TextView android:id="@+id/another_password_dialog_tv_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/what_password_did_you_use">
    </TextView>

    <EditText android:id="@+id/another_password_dialog_et_password"
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:inputType="textPassword">
        <requestFocus>
        </requestFocus>
    </EditText>

    <Button android:id="@+id/another_password_dialog_btn_ok"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="OK">
    </Button>

</LinearLayout>

你们知道为什么会发生这种异常吗?谢谢!

更新:如果我将一个类移动到一个单独的文件,没有这样的例外,一切顺利.所以问题是 – 当DialogFragment是一个内部类时,为什么会发生这种异常?

解决方法

尝试使内部类静态:
public static class FailedImportDialog extends DialogFragment

我会稍后再发布一些有关这方面的解释.在此期间,尝试这个,让我知道如果它的作品.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...