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();
Quelqu'un peut-il me dire le problème avec ce code donne l'exception .It ci-dessous:alerte Android Dialog ne fonctionne pas
11 au 6 novembre: 44: 20,572: ERREUR/AndroidRuntime (339): FATAL EXCEPTION: principal 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo {com.andoroid.dialog/com.andoroid.dialog.AlertDialogTestActivity}: android.view.WindowManager $ BadTokenException: Impossible d'ajouter une fenêtre - le jeton null n'est pas pour une application 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2663) 11 -06 11: 44: 20.572: ERREUR/AndroidRuntime (339): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2679) 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): à l'adresse android.app.ActivityThread.access $ 2300 (ActivityThread .java: 125) 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): à l'adresse android.app.ActivityThread $ H.handleMessage (ActivityThread.java:2033) 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): à l'adresse 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): à android.app.ActivityThread.main (ActivityThread.java:4627) 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): à java.lang.reflect.Method.invokeNative (méthode native) 11-06 11: 44: 20.572: ERROR/An droidRuntime (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): à l'adresse com.android.internal.os.ZygoteInit.main (ZygoteInit.java:626) 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): à dalvik.system.NativeStart.main (méthode native) 11-06 11: 44: 20.572: ERROR/AndroidRuntime (339): Causée par: android.view.WindowManager $ BadTokenException: Impossible d'ajouter window - token null n'est pas pour une 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): à 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.andoroid.dialog.AlertDialogTestActivity.createDialog (AlertDialogTestActivity.java:48) 11 -06 11: 44: 20.572: ERROR/AndroidRuntime (339): à com.andoroid.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): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2627)
Thanx beaucoup accouplent ça a marché !!! pouvez-vous me dire la raison de ce qui ne va pas dans l'utilisation de applicationContext. – Gaurav
public Contexte getApplicationContext() Depuis: API Niveau 1 Renvoie le contexte de l'unique objet Application global du processus en cours. Cela ne doit généralement être utilisé que si vous avez besoin d'un contexte dont le cycle de vie est distinct du contexte actuel, lié à la durée de vie du processus plutôt qu'au composant actuel. – breceivemail