trucos - pulpo android oreo
Error de Android: no se puede agregar la ventana: el token null no es para una aplicación (6)
Hy. Intenté crear un cuadro de diálogo de alerta, pero cuando ejecuto mi aplicación, se produce una excepción.
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)
Este es mi código:
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();
}
};
}
¿Alguien puede ayudarme?
A continuación se muestran las cosas que puede hacer para solucionar el problema.
En lugar de usar
context
Deberías usarActivityName.this
Si está usando getApplicationContext (), entonces use la marca de abajo antes de usar getApplicationContext ()
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
Agregue el siguiente permiso a su manifiesto:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Cambié getApplicationContext()
a
activity.this
funcionará
Cambié:
ProgressDialog(getApplicationContext());
a:
ProgressDialog(MainActivity.this);
,
En mi caso estaba tratando de crear mi diálogo así:
new Dialog(getApplicationContext());
Así que tuve que cambiar por:
new Dialog(MyActivity.this);
Y empezó a funcionar.
Enviar en actividad, en lugar de contexto.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
Para agregar AlertDialog
a su Activity
o Fragment
, debe usar la instancia de su Activity
, no la aplicación que está haciendo en su código. Crea tu AlertDialog
esta manera:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
Y eso debería hacer el truco por ti! :)