gpt4 book ai didi

android - 多个对话框,第二个不显示

转载 作者:太空狗 更新时间:2023-10-29 15:16:48 24 4
gpt4 key购买 nike

我试图在按下按钮时显示几个对话框,一个接一个,但无法显示第二个。第一个只是一个开发 stub ,用于在开发过程中替换验证过程...

@Override
public void onCreate(Bundle savedInstanceState)
{
<snip>

btnLogin.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
<snip>

VerifyLogin(userName,
password);
}
});
}

...和...

private void VerifyLogin(String userName,
String password)
{
boolean ok = false;

if ((userName.length() > 0) && (password.length() > 0))
ok = DevStub(); // <- Mimics server validation, allows dev. to choose success or not

<snip>
}

...和...

private boolean DevStub()
{
new AlertDialog.Builder(this)
.setTitle("DEVELOPMENT")
.setMessage("Login: Successful or Not")
.setPositiveButton("Successful",
<snip>
)
.setNegativeButton("Not Successful",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
new AlertDialog.Builder(getApplicationContext())
.setTitle("Invalid Username/Password")
.setMessage("Try Again or Recover your password")
.setPositiveButton( <snip> )
.setNegativeButton( <snip> )
.show();
}
})
.show();

return success;
}

正如我所说,这只是在我开发界面时,然后 DevStub() 将被真正的登录验证所取代,但我想了解我在这里出了什么问题。当我点击“不成功”按钮时,我只是得到一个异常。

我认为它与内部对话框中的 getApplicationContext() 有关,但话又说回来,我不知道 - 有什么建议吗?

日志:

E/SKIA(12620): FimgApiStretch:stretch failed
D/AndroidRuntime(12620): Shutting down VM
W/dalvikvm(12620): threadid=1: thread exiting with uncaught exception (group=0x40c581f8)
E/AndroidRuntime(12620): FATAL EXCEPTION: main
E/AndroidRuntime(12620): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime(12620): at android.view.ViewRootImpl.setView(ViewRootImpl.java:706)
E/AndroidRuntime(12620): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:316)
E/AndroidRuntime(12620): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:218)
E/AndroidRuntime(12620): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:143)
E/AndroidRuntime(12620): at android.app.Dialog.show(Dialog.java:278)
E/AndroidRuntime(12620): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
E/AndroidRuntime(12620): at com.mtbsoft.wud.LoginActivity$2.onClick(LoginActivity.java:62)
E/AndroidRuntime(12620): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:168)
E/AndroidRuntime(12620): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(12620): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(12620): at android.app.ActivityThread.main(ActivityThread.java:4514)
E/AndroidRuntime(12620): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(12620): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(12620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
E/AndroidRuntime(12620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
E/AndroidRuntime(12620): at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(12620): GC_CONCURRENT freed 148K, 4% free 13223K/13639K, paused 2ms+4ms

最佳答案

您不能使用应用程序 Context 创建一个 Dialog,它必须是一个 Activity Context,特别是 Activity 它所在的位置。

尝试这样的事情:

final Context context = this;
new AlertDialog.Builder(context)
.setTitle("DEVELOPMENT")
.setMessage("Login: Successful or Not")
.setPositiveButton("Successful",
<snip>
)
.setNegativeButton("Not Successful",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss(); //you probably want to add this...
new AlertDialog.Builder(context)
.setTitle("Invalid Username/Password")
.setMessage("Try Again or Recover your password")
.setPositiveButton( <snip> )
.setNegativeButton( <snip> )
.show();
}
})
.show();

return success;

另外,请注意我在 negativeButton OnClickListener 中添加了一个 dialog.dismiss()

关于android - 多个对话框,第二个不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12026740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com