【问题标题】:Android alert Dialog not workingAndroid警报对话框不起作用
【发布时间】:2011-11-06 06:18:44
【问题描述】:
AlertDialog.Builder builder;
    AlertDialog alertDialog;

    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.dialoglayout,
                                   (ViewGroup) findViewById(R.id.layout_root));

    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView) layout.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);

    builder = new AlertDialog.Builder(mContext);
    builder.setView(layout);
    alertDialog = builder.create();

    alertDialog.show();

谁能告诉我这段代码的问题。它给出了以下异常:

11-06 11:44:20.572: 错误/AndroidRuntime(339): 致命异常: main 11-06 11:44:20.572: 错误/AndroidRuntime(339): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.andoroid.dialog/com.andoroid.dialog.AlertDialogTestActivity}: android.view.WindowManager$BadTokenException : 无法添加窗口 -- 令牌 null 不适用于应用程序 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.os.Handler.dispatchMessage(Handler.java:99) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.os.Looper.loop(Looper.java:123) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 java.lang.reflect.Method.invoke(Method.java:521) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-06 11:44:20.572:错误/AndroidRuntime(339):在 dalvik.system.NativeStart.main(本机方法) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 由: android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.view.ViewRoot.setView(ViewRoot.java:509) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.Dialog.show(Dialog.java:241) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 com.andoroid.dialog.AlertDialogTestActivity.createDialog(AlertDialogTestActivity.java:48) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 com.andoroid.dialog.AlertDialogTestActivity.onCreate(AlertDialogTestActivity.java:22) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-06 11:44:20.572: 错误/AndroidRuntime(339): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    我的想法:

    1) 使用当前活动而不是mContext = getApplicationContext(); 例如:

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    

    this 指的是您在其中编写代码的活动。

    2) 清除您的项目

    【讨论】:

    • 非常感谢它的工作!你能告诉我使用 applicationContext 出错的原因吗?
    • public Context getApplicationContext () 自:API 级别 1 返回当前进程的单个全局应用程序对象的上下文。这通常只应在您需要一个生命周期与当前上下文分离的上下文时使用,该上下文与进程的生命周期而不是当前组件相关联。
    【解决方案2】:

    此外,如果您想要自定义对话框,则无需膨胀视图并使用 AlertDialog.Builder。

    相反,您可以这样做:

    Dialog customDialog = new Dialog(YourActivity.this);
    customDialog.setContentView(R.layout.dialoglayout);
    TextView text = (TextView) customDialog.findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView) customDialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);
    
    customDialog.show();
    

    您可以在 Android 开发指南中看到这样的示例: http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2011-12-25
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多