android - AlertDialog 使应用程序崩溃

标签 android android-alertdialog

有人可以向我解释为什么这个 AlertDialog 会崩溃吗?

package com.clicker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Clicker extends Activity
{
    public int clickerNumber = 0;
    private TextView clickerText;
    private Button clickerButton;
    private Button resetButton;

    // Called when the activity is first created.
    @SuppressWarnings("null")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Declare each of the layout objects
        clickerText = (TextView)findViewById(R.id.clickerText);
        clickerButton = (Button)findViewById(R.id.clickerButton);
        resetButton = (Button)findViewById(R.id.resetButton);

        clickerText.setText("0");

        final AlertDialog.Builder resetQuestion = null;
        resetQuestion.setTitle("Reset?");
        resetQuestion.setMessage("Are you sure you want to reset the counter?");

        resetQuestion.setPositiveButton("Yes", new OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                    clickerNumber = 0;
                    clickerText.setText("0");
            }

        });

        resetQuestion.setNegativeButton("No", new OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            } 

        });

        clickerButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                clickerNumber++;
                clickerText.setText(Integer.toString(clickerNumber));
            }
        });

        resetButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                resetQuestion.show();
            }

        });
    };
};

最佳答案

这是一个巨大的失败:

final AlertDialog.Builder resetQuestion = null;
resetQuestion.setTitle("Reset?");

您正在尝试使用空对象,并且(当然)会抛出 NullPointerException

这就是我创建对话框的方式(我认为这是最好的方式):

LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialogo_layout, null);
final AlertDialog.Builder resetQuestion = new AlertDialog.Builder(YourActivity.this)
// do whatever you want with the resetQuestion AlertDialog

此处,R.layout.dialogo_layout 表示 res/layout 目录中名为 dialogo_layout.xml 的文件,其中包含对话框布局。

关于android - AlertDialog 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3061673/

相关文章:

android - 将 onTouch ACTION_MOVE 限制为一个 View

android - 在 GridView 上实现滑动手势

android - 从另一个类调用对话框

android - 具有多个倒数计时器的 Recyclerview 会导致闪烁

java - ANDROID:权限请求代码未出现

java - 安卓警报 : how to upgrade AlertDialog from old style (black) to new style (white)?

android - 在 fragment 中找不到警报对话框的 View

android - 警报对话框文本颜色的 XML 属性

安卓错误 : Unable to add window -- token null is not for an application

android - 在 Android Studio 默认模板中更改抽屉导航图标的颜色