Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android警报对话框不工作_Android_Android Alertdialog - Fatal编程技术网

Android警报对话框不工作

Android警报对话框不工作,android,android-alertdialog,Android,Android Alertdialog,有人能告诉我这个代码的问题吗?它给出了以下例外情况: 11-06 11:44:20.572:错误/AndroidRuntime(339):致命异常:主 11-06 11:44:20.572:错误/AndroidRuntime(339):java.lang.RuntimeException:无法启动活动组件信息{com.android.dialog/com.android.dialog.AlertDialogTestActivity}:android.view.WindowManager$BadT

有人能告诉我这个代码的问题吗?它给出了以下例外情况:

11-06 11:44:20.572:错误/AndroidRuntime(339):致命异常:主 11-06 11:44:20.572:错误/AndroidRuntime(339):java.lang.RuntimeException:无法启动活动组件信息{com.android.dialog/com.android.dialog.AlertDialogTestActivity}:android.view.WindowManager$BadTokenException:无法添加窗口--标记null不适用于应用程序 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.os.Handler.dispatchMessage(Handler.java:99)上 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.os.Looper.loop(Looper.java:123)上 11-06 11:44:20.572:ERROR/AndroidRuntime(339):位于android.app.ActivityThread.main(ActivityThread.java:4627) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):位于java.lang.reflect.Method.invokenactive(本机方法) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):位于java.lang.reflect.Method.invoke(Method.java:521) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-06 11:44:20.572:ERROR/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:ERROR/AndroidRuntime(339):在android.view.ViewRoot.setView(ViewRoot.java:509) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.app.Dialog.show(Dialog.java:241)上 11-06 11:44:20.572:ERROR/AndroidRuntime(339):位于com.android.dialog.AlertDialogTestActivity.createDialog(AlertDialogTestActivity.java:48) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):位于com.android.dialog.AlertDialogTestActivity.onCreate(AlertDialogTestActivity.java:22) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)上 11-06 11:44:20.572:ERROR/AndroidRuntime(339):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)上

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

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();
如果您正在编写代码,则此指的是您的活动


2) 清除项目

此外,如果需要自定义对话框,则无需放大视图并使用AlertDialog.Builder

相反,您可以这样做:

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
您可以在《Android开发指南》中看到这样一个示例:

thanx很多伴侣都成功了!!!您能告诉我使用applicationContext.public Context getApplicationContext()的错误原因吗?因为:API级别1返回当前进程的单个全局应用程序对象的上下文。通常,只有当您需要一个其生命周期与当前上下文分离的上下文时,才应该使用该上下文,该上下文与流程的生命周期而不是当前组件相关联。
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();