如何在另一个活动中显示警报对话框?

时间:2014-04-03 13:24:46

标签: android

我想向主要活动类的getversion方法方法显示一个Alert Dialog非活动类,但每当我通过该方法调用AlertDialog时,它会生成带有以下文本的错误

public void AlertUpgrade(Activity activity) {

    Log.e("AlertUpgrade", "Communicator.");
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage("  Click OK to Upgrade Now ? ")
        .setPositiveButton("OK" +
            "", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            Log.e("onClick", "AlertUpgrade");
            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=gsip.webgalaxy"));
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            app_context.startActivity(marketIntent);  
        }
    }).setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                return false;
            }

            return true;
        }
    });
    builder.create().show();  
}

public void AlertUpdate(Activity activity) {

    Log.e("AlertUpdate", "Communicator.....");
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage("There is newer version of this application available, click OK to update now?").setPositiveButton("OK" +
        "", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            Log.e("onClick", "AlertUpdate");
            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("url"));
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            app_context. startActivity(marketIntent);  
        }
    }).setNegativeButton("Remind Later", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            // User cancelled the dialog
        }

    }).setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
               if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                   return false;
               }

               return true;
           }
       });
    builder.create().show(); 
}

错误记录

04-03 09:04:44.007: E/AndroidRuntime(2249): java.lang.RuntimeException: Unable to start activity ComponentInfo{gsip.webgalaxy/gsip.webgalaxy.ui.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.os.Looper.loop(Looper.java:136)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at java.lang.reflect.Method.invoke(Method.java:515)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at dalvik.system.NativeStart.main(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Dialog.show(Dialog.java:286)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at gsip.webgalaxy.ui.Communicator.getversion(Communicator.java:226)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at gsip.webgalaxy.ui.MainActivity.onCreate(MainActivity.java:460)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Activity.performCreate(Activity.java:5231)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 09:04:44.007: E/AndroidRuntime(2249):     ... 11 more
04-03 09:09:44.177: I/Process(2249): Sending signal. PID: 2249 SIG: 9

2 个答案:

答案 0 :(得分:2)

一种可能性是在启动另一个Activity时指定一些额外内容,然后在目标Activity中截取这些额外内容并相应地显示AlertDialog

像这样......

来源 Activity

  Intent intent = new Intent(this, TargetActivity.class);
  Bundle bundle = new Bundle();
  bundle.putInt("alert_icon_res_id", android.R.drawable.ic_dialog_info);
  bundle.putString("alert_title", "Some Title");
  bundle.putString("alert_message", "Some message");
  intent.putExtras(bundle);
  startActivity(intent);

目标 Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Bundle b = getIntent().getExtras();
  if (b != null && b.containsKey("alert_icon_res_id")) {
    int icon = b.getInt("alert_icon_res_id");
    int title = b.getString("alert_title");
    int message = b.getString("alert_message");

    new AlertDialog.Builder(this).setIcon(icon)
      .setTitle(title).setMessage(message)
      .setPositiveButton(R.string.edit_dialog_ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
        }
      }).create().show());
   }
}

答案 1 :(得分:0)

您需要Activity Context对话框。如果您要从非活动类中显示它,请将对Activity的引用作为参数传递。