support.v7.app.AlertDialog在dismiss上抛出NullPointerException

时间:2015-04-23 06:11:12

标签: android alertdialog android-appcompat

我将AppCompat更新为新发布的修订版22.1.0,并将我的AlertDialog更改为support.v7.app.AlertDialog。但是在Lollipop设备上,它会在dismissDialog()上抛出以下异常。

java.lang.NullPointerException:   attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
  at android.support.v7.internal.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:248)
  at android.support.v7.internal.app.WindowDecorActionBar.init(WindowDecorActionBar.java:201)
  at android.support.v7.internal.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:184)
  at android.support.v7.app.AppCompatDeleg  ateImplV7.cre  ateSupportActionBar(AppCompatDelegateImplV7.java:176)
  at android.support.v7.app.AppCompatDelegateImplBase.getSupportActionBar(AppCompatDelegateImplBase.java:85)
  at android.support.v7.app.AppCompatDelegateImplV7.onStop(AppCompatDeleg  ateImplV7.java:221)
  at android.support.v7.app.AppCompatDialog.onStop(AppCompatDialog.java:108)
  at android.app.Dialog.dismissDialog(Dialog.java:438)
  at android.app.Dialog.dismiss(Dialog.java:414)
  at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5834)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

我该如何解决?

(较低版本的设备似乎运行良好。这恰好发生在Lollipop上)

+ 我没有在我的代码中明确地调用dismiss()。当按钮被后退按钮或正/负按钮关闭时,该对话框会抛出异常。

++ 以下是我使用v7.app.AlertDialog的代码。谢谢。

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;

public class SimpleYesNoFragment extends DialogFragment {

    public interface OnConfirmListner {
        public void onConfirm();
    }

    public static SimpleYesNoFragment newInstance(String title, String message) {
        SimpleYesNoFragment fragment = new SimpleYesNoFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("message", message);
        fragment.setArguments(args);

        return fragment;
    }

    private OnConfirmListner mListener;

    public void setOnConfirmListener(OnConfirmListner l) {
        mListener = l;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder b=  new  AlertDialog.Builder(getActivity())
        .setPositiveButton(R.string.yes, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(mListener != null) {
                    mListener.onConfirm();
                }
            }
        })
        .setNegativeButton(R.string.no, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        Bundle args = getArguments();
        String title = args.getString("title",null);
        if(!TextUtils.isEmpty(title)) {
            b.setTitle(title);
        }
        String message = args.getString("message",null);
        if(!TextUtils.isEmpty(message)) {
            b.setMessage(message);
        }

        return b.create();
    }
}

1 个答案:

答案 0 :(得分:6)

我偶然发现我的项目有values-v21/themes.xml,并且android:Theme.Material.Light.Dialog.Alert适用于support.v7.app.AlertDialog。这引起了奇怪的错误。

使用适当的AppCompat主题(例如Theme.AppCompat.Light.Dialog.Alert)来解决问题support.v7.app.AlertDialog。感谢大家帮助我。