i++

プログラム系のメモ書きなど

Android : (AppCompat) Dialog の NullPointerException at android.support.v7.internal.app.WindowDecorActionBar.getDecorToolbar

ActionBar を使う Theme を指定すると発生しました。

全画面 Dialog を作ろうと "Theme.AppCompat.Light.DialogWhenLarge" を使ってこのエラーを出してしまっていたが、"Theme.AppCompat.Light.NoActionBar" に変える事で解消です。見た目に違いはありません。 もしくは、以下のように ActionBar を使わない設定を style に追加すればエラーが出なくなります。

<style name="EditDialogTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
    <item name="android:windowActionBar">false</item>
    <item name="windowActionBar">false</item>
</style>

Issue 170017 - android - AlertDialog causes NullPointerException when using FEATURE_ACTION_BAR - Android Open Source Project - Issue Tracker - Google Project Hosting

Java

  • DialogFragment は support.v4, AlertDialog は support.v7
  • style は AlertDialog.Builder の第2引数で指定
  • onCreateView では null を返す
public class EditDialogFragment extends DialogFragment {
    // constructor 等省略...
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.EditDialogTheme);
        View view = View.inflate(getActivity(), R.layout.edit_dialog, null);
        // データの設定とか...
        builder.setView(view);
        return builder.create();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }
}