Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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

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
Java 从多用途对话框返回结果_Java_Android - Fatal编程技术网

Java 从多用途对话框返回结果

Java 从多用途对话框返回结果,java,android,Java,Android,因此,在我的android应用程序中,我有一个助手类,它可以使用以下代码创建一个对话框: public void CreateAlert(String title, String message) { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); // Setting Dialog Title alertDialog.setTitle(title); // Settin

因此,在我的android应用程序中,我有一个助手类,它可以使用以下代码创建一个对话框:

public void CreateAlert(String title, String message)
{
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which) { }
    });

    // Showing Alert Message
    alertDialog.show();
}
在我的注册器活动中,我有一个ASyncTask,它运行一个特定的任务,然后用下面的函数处理一个布尔变量:

private void Process(boolean Success)
{
    if(Success)
    {               
        appHelper.CreateAlert("Success!", "Well Done!");

        // Progress to the activity
        startActivity( new Intent(context, Menu.class) );               
        getActivity().finish();
    }
    else
    {
        appHelper.CreateAlert("Failure!", "Please try again.");
    }
}
但是,正如您从上面的代码中看到的,我希望基于成功的结果启动一个活动。但是,我需要应用程序等待用户单击对话框上的OK按钮,然后再进行下一个活动

最好的方法是什么


编辑:我应该注意到这两个函数在不同的类中,CreateAlert在一个名为AppHelper的类中,其中as Process位于Registrar.class(一个活动)中。

再次检查alertDialog的单击侦听器的
确定
按钮中布尔
成功的状态

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which) {

    if(Success)
    {               
        // Progress to the activity
        context.startActivity( new Intent(context, Menu.class) );               
        context.getActivity().finish();
    }
    else
    {
        // Don't do anything
    } 
    });
并更新您的
流程
,如下所示

private void Process(boolean Success)
{
    if(Success)
    {               
        appHelper.CreateAlert("Success!", "Well Done!");

     }
    else
    {
        appHelper.CreateAlert("Failure!", "Please try again.");
    }
}

@user2990037检查更新的答案,您需要在alertdialog类中传递活动的上下文以启动新活动。很抱歉,我认为该解决方案不起作用:(void进程位于我的主类中,它需要能够找出“确定”按钮的时间(侦听器在apphelper类中注册)已经被按下。试试这个就行了。告诉我你在传递上下文方面是否需要任何帮助。啊,是的,我现在明白你的意思了哈哈,对不起,这是漫长的一天lol。我从getActivity()得到一个错误,它显然未定义类型上下文。还有一种方法可以将类传递给CreateAlert吗?是的,您可以创建一个对象实例化它并作为参数传递。