如何在简单的类中使用alertdialog?

时间:2013-08-27 09:56:20

标签: android android-alertdialog

我在一个简单的类中创建一个警告框,但我不知道是什么问题。我的代码如下。当我运行我的活动并且我想运行我的alertdialog应用程序崩溃。

private class ApplicationLauncher implements
            AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(final AdapterView parent, View v,
                final int position, long id) {
            // //////////////////////////////////////////////////////////
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    getApplicationContext());
            builder.setCancelable(true);
            builder.setTitle("TestsAuthen");
            builder.setInverseBackgroundForced(true);
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ApplicationInfo app = (ApplicationInfo) parent
                                    .getItemAtPosition(position);
                            startActivity(app.intent);
                        }
                    });
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

            // //////////////////
        }
    }

2 个答案:

答案 0 :(得分:0)

您正在尝试使用getApplicationContext().构建alertDialog 您需要将活动上下文传递给它。

private class ApplicationLauncher implements
            AdapterView.OnItemClickListener {

        Context context;

        public ApplicationLauncher(Context context){
          this.context = context;
        }
        @Override
        public void onItemClick(final AdapterView parent, View v,
                final int position, long id) {
            // //////////////////////////////////////////////////////////
            AlertDialog.Builder builder = new AlertDialog.Builder(
                   context);
            builder.setCancelable(true);
            builder.setTitle("TestsAuthen");
            builder.setInverseBackgroundForced(true);
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ApplicationInfo app = (ApplicationInfo) parent
                                    .getItemAtPosition(position);
                            startActivity(app.intent);
                        }
                    });
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

            // //////////////////
        }
    }

现在只需将您的上下文this传递给构造函数,如

  new ApplicationLauncher(this);

答案 1 :(得分:0)

向您的类添加一个构造函数,该类初始化本地上下文;

public class ApplicationLauncher implements AdapterView.OnItemClickListener {

private Context context;

public ApplicationLauncher(Context context) {
    this.context = context;
}
...

}

在您的活动中将此类实例化为;

 ApplicationLauncher al = new ApplicationLauncher( this );