我正在尝试按下按钮时显示几个对话框,一个接一个,并且无法显示第二个按钮。第一个是在开发过程中替换验证过程的开发存根...
@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()有关,但是,我不知道 - 请问有什么建议吗?
logcat的:
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
答案 0 :(得分:0)
您无法使用应用Dialog
创建Context
,它必须是活动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 dialog.dismiss()
中添加了OnClickListener
。