4
4

More than 5 years have passed since last update.

DialogFragmentの表示でハマったこと

Posted at

AlertDialogFragmentを定義しました

ちゃんと setArgumentsでデータを渡しました.
ちゃんと publicなデフォルトコンストラクタ用意しました.

でも画面回転で落ちた.

android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.test.MainActivity$1: make sure class name exists, is public, and has an empty constructor that is public

え?publicなデフォルトコンストラクタを用意しろって?あれ?

呼び出し側がマズかった

MainActivity.java
AlertDialogFragment dialog = new AlertDialogFragment() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        // dismiss で処理をしたかった
        super.onDismiss(dialog);
    }
};
dialog.setCancelable(false);
Bundle bundle = new Bundle();
bundle.putString("title", "test");
bundle.putString("message", "test");
bundle.putString(AlertDialogFragment.ARG_LABEL_POSITIVE, "yes");
bundle.putString(AlertDialogFragment.ARG_LABEL_NEGATIVE, "no");
dialog.setArguments(bundle);
getFragmentManager().beginTransaction().add(dialog, "TEST").commitAllowingStateLoss();

あ゛っ!

MainActivity.java
AlertDialogFragment dialog = new AlertDialogFragment() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        // dismiss で処理をしたかった
        super.onDismiss(dialog);
    }
};

そりゃ,匿名クラスにしちゃったら作成したAlertDialogFragmentとはベツもんだよね.
だから,再生成の際にコンストラクタがないよって言われちゃったのね.
この辺はうっかりやっちゃうよね(おれだけ?)

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4