活动从片段返回结果后,从活动中显示警报对话框

时间:2013-01-24 20:24:53

标签: android

标题可能不是很清楚,在短标题中难以解释。但这是问题所在。

我有一个活动MainActivity,然后在展示FragmentA时显示活动。在MainActivity中,显示了一个可通过FragmentA访问的菜单。在FragmentA的菜单中,用户单击Generate Password,然后调用另一个名为GeneratePassword的活动,该活动使用startActivityForResult进行调用。当用户按下提交按钮时,用户随后被发送回FragmentA和onActivityResult内的函数MainActivity。在这个函数中,我然后使用一个公共类,它允许我显示带有Yes和No Buttons的AlertDialog。如果我取出common.showYesNoDialog()if语句,OnActivityResult工作正常,但是当使用它时我得到一个异常

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

以下是启动GeneratePassword活动的代码

Intent generatePasswrodIntent = new Intent(mMainActivity, GeneratePassword.class);
                startActivityForResult(generatePasswrodIntent, AddNewLogin.GENERATE_PASSWORD);
                return(true);

以下是设置GeneratePassword活动结果的代码

Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("password", generatePassword.generatePassword());
        intent.putExtras(bundle);
        setResult(AddNewLogin.GENERATE_PASSWORD, intent);
        finish();

以下是onActivityResult中显示警告对话框的代码

Bundle bundle = data.getExtras();

        String password = bundle.getString("password");
        Log.d("PASSWORD", password);
        if (common.showYesNoDialog("Your Generated Password Is:\n " + password + "\nDo you want to copy this to the clipboard?", false))
        {
            ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(password);
        }

下面是YesNoDialog函数,但我知道这很好,因为我在我的应用程序中的几个地方使用它没有问题。

final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message mesg)
            {
                throw new RuntimeException();
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(isCancelable);
            builder.setMessage(message)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialogResult = true;
                    handler.sendMessage(handler.obtainMessage());
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialogResult = false;
                    handler.sendMessage(handler.obtainMessage());
                }
            });
        AlertDialog alert = builder.create();
        alert.show();
        try
        {
            Looper.loop();
        }
        catch (RuntimeException ex)
        {
            Log.d("Dialog Error", ex.toString());
        }
        return dialogResult;

以下是我如何初始化公共类

Common common = new Common(getApplicationContext());

我尝试使用this而不是getApplicationContext,但没有任何区别。

我只是不明白这里发生了什么。

感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:0)

问题出在showYesNoDialog()。 showYesNoDialog()方法的布尔参数有什么作用?

相关问题