如何在Android应用程序中显示警报消息

时间:2015-11-23 04:11:25

标签: android

我正在尝试显示提醒消息。 但是,当我单击特定按钮以显示警告消息时,应用程序停止运行。

我的警报信息代码在这里......

search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Cursor res = myDB.getAllData();
            if (res.getCount() == 0) {
                showMessage("Error", "Nothing found");
                return;
            }

            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext()) {
                buffer.append("EMAIL: " + res.getString(0) + "\n");
                buffer.append("full_name : " + res.getString(1) + "\n");
                buffer.append("district : " + res.getString(2) + "\n");
                buffer.append("phone_num : " + res.getString(3) + "\n");

            }
            showMessage("Data", buffer.toString());
        }



    });
}

public void showMessage(String title, String message)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();

}

4 个答案:

答案 0 :(得分:1)

试试这段代码:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder1.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();

答案 1 :(得分:0)

而不是

AlertDialog.Builder builder = new AlertDialog.Builder(this);

使用此

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); // for activity

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // for fragment

答案 2 :(得分:0)

public static void showAlert(final Activity activity, String title,
            String message, final boolean finish) {
        new AlertDialog.Builder(activity).setTitle(title).setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        if (finish) {
                            activity.finish();
                        }
                    }
                }).show();
    }

答案 3 :(得分:0)

我测试了showMessage方法。它没有任何问题。我想你必须检查数据库连接cursorbuffer。但是,此代码用于显示警告消息:

/**
 * @param context
 * @param title
 * @param message
 * 
 *            Caution:Error--> android.view.WindowManager$BadTokenException:
 *            Unable to add window -- token null is not for an application
 * 
 *            --> Instead of getApplicationContext(), just use
 *            ActivityName.this
 */
public static AlertDialog showDialog(final Context context, String title,
        String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setCancelable(true);
    builder.setMessage(message);
    builder.setTitle(title);
    builder.setPositiveButton(R.string.ok,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    return builder.show();
}