Android的警告对话框中不工作 [英] Android alert Dialog not working
问题描述
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();
谁能告诉我这个$ C $的题C。它提供了以下异常:
Can anyone tell me the problem with this code .It gives the below exception:
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):致命异常:主要
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.andoroid.dialog / com.andoroid.dialog.AlertDialogTestActivity}:android.view.WindowManager $ BadTokenException :无法添加窗口 - 令牌null不是一个应用程序
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.os.Handler.dispatchMessage(Handler.java:99)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.os.Looper.loop(Looper.java:123)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.ActivityThread.main(ActivityThread.java:4627)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在java.lang.reflect.Method.invokeNative(本机方法)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在java.lang.reflect.Method.invoke(Method.java:521)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在dalvik.system.NativeStart.main(本机方法)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):$ android.view.WindowManager BadTokenException:产生的原因无法添加窗口 - 令牌null不是一个应用程序
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.view.ViewRoot.setView(ViewRoot.java:509)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.Dialog.show(Dialog.java:241)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在com.andoroid.dialog.AlertDialogTestActivity.createDialog(AlertDialogTestActivity.java:48)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在com.andoroid.dialog.AlertDialogTestActivity.onCreate(AlertDialogTestActivity.java:22)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11月11日至6日:44:20.572:ERROR / AndroidRuntime(339):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-06 11:44:20.572: ERROR/AndroidRuntime(339): FATAL EXCEPTION: main 11-06 11:44:20.572: ERROR/AndroidRuntime(339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andoroid.dialog/com.andoroid.dialog.AlertDialogTestActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.os.Handler.dispatchMessage(Handler.java:99) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.os.Looper.loop(Looper.java:123) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at java.lang.reflect.Method.invokeNative(Native Method) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at java.lang.reflect.Method.invoke(Method.java:521) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at dalvik.system.NativeStart.main(Native Method) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.view.ViewRoot.setView(ViewRoot.java:509) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.Dialog.show(Dialog.java:241) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at com.andoroid.dialog.AlertDialogTestActivity.createDialog(AlertDialogTestActivity.java:48) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at com.andoroid.dialog.AlertDialogTestActivity.onCreate(AlertDialogTestActivity.java:22) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-06 11:44:20.572: ERROR/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
推荐答案
我的想法:
1)使用当前的活动,而不是 mContext = getApplicationContext();
例如:
1) use current activity instead of mContext = getApplicationContext();
for example:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
这个指的是你的活动,如果你在它写code。
this refers to your activity if you are writing code in it.
2)清除您的项目
这篇关于Android的警告对话框中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!