Hy. J'ai essayé de créer une boîte de dialogue d'alerte mais lorsque j'exécute mon application, je lance une exception
09-26 12:43:21.949: E/AndroidRuntime(14618): FATAL EXCEPTION: main
09-26 12:43:21.949: E/AndroidRuntime(14618): Android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewRootImpl.setView(ViewRootImpl.Java:687)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.WindowManagerImpl.addView(WindowManagerImpl.Java:301)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.WindowManagerImpl.addView(WindowManagerImpl.Java:215)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.Java:140)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.app.Dialog.show(Dialog.Java:278)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.example.stampp.UI.Rewards.DeleteRewardsFragment$1.onItemClick(DeleteRewardsFragment.Java:80)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.widget.AdapterView.performItemClick(AdapterView.Java:292)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.widget.AbsListView.performItemClick(AbsListView.Java:1394)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.widget.AbsListView$PerformClick.run(AbsListView.Java:3024)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.widget.AbsListView.onTouchEvent(AbsListView.Java:3846)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.View.dispatchTouchEvent(View.Java:5629)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1964)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1725)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.Java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewGroup.dispatchTouchEvent(ViewGroup.Java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.Android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.Java:2062)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.Android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.Java:1400)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.app.Activity.dispatchTouchEvent(Activity.Java:2369)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.Android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.Java:2010)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.View.dispatchPointerEvent(View.Java:5809)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.Java:3130)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewRootImpl.handleMessage(ViewRootImpl.Java:2658)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewRootImpl.processInputEvents(ViewRootImpl.Java:1015)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.view.ViewRootImpl.handleMessage(ViewRootImpl.Java:2667)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.os.Handler.dispatchMessage(Handler.Java:99)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.os.Looper.loop(Looper.Java:137)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Android.app.ActivityThread.main(ActivityThread.Java:4517)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Java.lang.reflect.Method.invokeNative(Native Method)
09-26 12:43:21.949: E/AndroidRuntime(14618): at Java.lang.reflect.Method.invoke(Method.Java:511)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:993)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:760)
09-26 12:43:21.949: E/AndroidRuntime(14618): at dalvik.system.NativeStart.main(Native Method)
Voici mon code:
public class DeleteRewardsFragment extends Fragment{
private Context context;
private FragmentsActivity activity;
private ListView listView;
private View myFragmentView;
private DeleteRewardsAdapter adapter;
private String[] values;
@SuppressLint("ResourceAsColor")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.listview, container, false);
listView = (ListView) myFragmentView.findViewById(Android.R.id.list);
activity = (FragmentsActivity) getActivity();
context = activity.getApplicationContext();
values = new String[4];
adapter = new DeleteRewardsAdapter(context, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(deleteEvent);
return myFragmentView;
}
private OnItemClickListener deleteEvent = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog .setTitle("Jetzt löschen?");
alertDialog .setMessage("Die in deisem Store gesammelten stampss gehen debei verloren!")
.setCancelable(false)
.setPositiveButton("Abbrechen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
values = new String[values.length-1];
adapter = new DeleteRewardsAdapter(context, values);
listView.setAdapter(adapter);
}
})
.setNegativeButton("Löschen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = alertDialog.create();
dialog.show();
}
};
}
Quelqu'un peut-il m'aider?
Pour ajouter AlertDialog
à votre Activity
ou Fragment
vous devez utiliser l'instance de votre Activity
, pas celle de votre application que vous faites dans votre code. Créez votre AlertDialog
comme ceci:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
Et cela devrait faire l'affaire pour vous! :)
Dans mon cas, j'essayais de créer ma boîte de dialogue comme ceci:
new Dialog(getApplicationContext());
J'ai donc dû changer pour:
new Dialog(MyActivity.this);
Et ça a commencé à fonctionner.
Vous trouverez ci-dessous ce que vous pouvez faire pour résoudre le problème.
À la place d'utiliser
context
Tu devrais utiliser ActivityName.this
si vous utilisez getApplicationContext (), utilisez l'indicateur ci-dessous avant d'utiliser getApplicationContext ()
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
Ajoutez l'autorisation suivante à votre manifeste:
<uses-permission Android:name="Android.permission.SYSTEM_ALERT_WINDOW" />
Envoyez l'activité plutôt que le contexte.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
J'ai changé:
ProgressDialog(getApplicationContext());
à:
ProgressDialog(MainActivity.this);
,
J'ai changé getApplicationContext()
en
activity.this
ça va marcher