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

前端之家收集整理的这篇文章主要介绍了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

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

原文链接:https://www.f2er.com/android/311910.html

猜你在找的Android相关文章