3

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

@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
4

1 回答 1

0

您不能Dialog使用 Application创建一个Context,它必须是一个 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;

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

于 2012-08-19T13:53:43.757 回答