0

我在一个简单的类中创建了一个警报框,但我不知道问题出在哪里。我的代码如下。当我运行我的活动并且我想运行我的 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();

            // //////////////////
        }
    }
4

2 回答 2

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);
于 2013-08-27T10:08:31.990 回答
0

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

public class ApplicationLauncher implements AdapterView.OnItemClickListener {

private Context context;

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

}

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

 ApplicationLauncher al = new ApplicationLauncher( this );
于 2013-08-27T10:13:38.470 回答