【问题标题】:Getting exception when creating AlertDialog android创建AlertDialog android时出现异常
【发布时间】:2015-07-27 21:37:19
【问题描述】:

我收到错误:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

它出现在我的 ErrorDialogFragment 正在执行 builder.create();

这是我的 ErrorDialogFragment 代码:

public class ErrorDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    String title = getArguments().getString("title");
    String message = getArguments().getString("message");


    View dialogView = inflater.inflate(R.layout.dialog_error, null);
    TextView tvTitle = (TextView) dialogView.findViewById(R.id.tvTitle);
    TextView tvMessage = (TextView) dialogView.findViewById(R.id.tvMessage);
    Button btnOk = (Button) dialogView.findViewById(R.id.btnOk);

    tvTitle.setText(title);
    tvMessage.setText(message);
    btnOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });

    builder.setView(dialogView);

    return builder.create();
}

}

调用 ErrorDialogFragment 的活动扩展了Activity,我需要它以android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" 为主题全屏显示。并且需要那个主题,否则 UI 会以一种奇怪的方式工作。

有什么想法吗?

【问题讨论】:

  • 您是否尝试在清单中的 Activity 而不是应用程序中设置主题?
  • 你能显示R.layout.dialog_error的布局文件吗?

标签: android android-alertdialog android-appcompat illegalstateexception


【解决方案1】:

使用android.app.DialogFragment 代替android.support.v7.app.AlertDialog

【讨论】:

  • 你可以在评论中问这个问题。
  • 这是问题所在。我使用的是android.support.v7.app.AlertDialog,而不是直接使用android.app.DialogFragment。谢谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
相关资源
最近更新 更多