10

我在班级中有一个DialogFragment定义为内部Fragment班级。在方向更改时,甚至会弹出以下异常:

 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内部类时?

4

3 回答 3

7

调用 setRetainInstance(true) 将导致 FragmentManager 保存您的实际 Fragment 实例。它不会销毁和重新创建 Fragment,而是将相同的片段传递给新的 Activity。

于 2012-09-20T10:23:44.333 回答
5

尝试使内部类静态:

public static class FailedImportDialog extends DialogFragment 

我会在一段时间内发布更多关于此的解释。与此同时,试试这个,让我知道它是否有效。

于 2012-07-18T18:41:07.870 回答
0

像这样定义你的内部类:

public class FailedImportDialog extends DialogFragment {
....
}

用公共符号。

因为,这就是您的 LogCat 所说的:

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
于 2012-07-18T18:00:36.967 回答