Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在简单类中创建alertdialog?_Android_Android Alertdialog - Fatal编程技术网

Android 如何在简单类中创建alertdialog?

Android 如何在简单类中创建alertdialog?,android,android-alertdialog,Android,Android Alertdialog,我在一个简单的类中创建了一个alertbox,但我不知道问题出在哪里。我的代码如下。当我运行“活动”并希望运行alertdialog时,应用程序会崩溃 private class ApplicationLauncher implements AdapterView.OnItemClickListener { @Override public void onItemClick(final AdapterView parent, View v

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

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

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

试一试

现在只需将上下文
这个
传递给构造函数

  new ApplicationLauncher(this);

向类添加构造函数,该构造函数初始化本地上下文

public class ApplicationLauncher implements AdapterView.OnItemClickListener {

private Context context;

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

}
在活动中将该类实例化为

 ApplicationLauncher al = new ApplicationLauncher( this );

崩溃使用getApplicationContext将堆栈跟踪张贴到logcat错误报告上,就像这样不是一个好主意将什么作为上下文添加一个
构造函数
,将
上下文
作为参数添加到此类,并在实例化此类时传递活动上下文,并在这个类中使用这个上下文。我还做了一些其他的事情,我创建了一个带有Activity类型的公共静态变量。在我的主要活动中,然后我将其用作上下文,它就起作用了。公共静态活动;然后fa=在我的主要活动的oncreat方法中是这样的。你试过这个吗?
 ApplicationLauncher al = new ApplicationLauncher( this );