Android 1.6:
Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
I'm trying to open a dialog window, but every time I try to open it it throws this exception:
Uncaught handler: thread main exiting due to uncaught exception android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRoot.setView(ViewRoot.java:460) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) at android.app.Dialog.show(Dialog.java:238) at android.app.Activity.showDialog(Activity.java:2413)
I'm creating it by calling showDialog
with the display's id. The onCreateDialog
handler logs fine and I can step through it without an issue, but I've attached it since it seems like I'm missing something:
@Override public Dialog onCreateDialog(int id) { Dialog dialog; Context appContext = this.getApplicationContext(); switch(id) { case RENAME_DIALOG_ID: Log.i("Edit", "Creating rename dialog..."); dialog = new Dialog(appContext); dialog.setContentView(R.layout.rename); dialog.setTitle("Rename " + noteName); break; default: dialog = null; break; } return dialog; }
Is there something missing from this? Some questions have talked about having this problem when creating a dialog from onCreate
, which happens because the activity isn't created yet, but this is coming from a call from a menu object, and the appContext
variable seems like it is correctly populated in the debugger.
Answer by Samuh for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
You cannot display an application window/dialog through a Context that is not an Activity. Try passing a valid activity reference
Answer by Torp for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
i.e. this line: Context appContext = this.getApplicationContext(); must go, and instead you use a pointer to the activity you're in (probably this).
I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from developer.android.com :(
Answer by kenyee for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Ditto on the getApplicationContext thing.
The documents on the android site says to use it, but it doesn't work...grrrrr :-P
Just do:
dialog = new Dialog(this);
"this" is usually your Activity from which you start the dialog.
Answer by T.Hawk for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
I had a similar issue where I had another class something like this:
public class Something { MyActivity myActivity; public Something(MyActivity myActivity) { this.myActivity=myActivity; } public void someMethod() { . . AlertDialog.Builder builder = new AlertDialog.Builder(myActivity); . AlertDialog alert = builder.create(); alert.show(); } }
Worked fine most of the time, but sometimes it crashed with the same error. Then I realise that in MyActivity
I had...
public class MyActivity extends Activity { public static Something something; public void someMethod() { if (something==null) { something=new Something(this); } } }
Because I was holding the object as static
, a second run of the code was still holding the original version of the object, and thus was still referring to the original Activity
, which no long existed.
Silly stupid mistake, especially as I really didn't need to be holding the object as static
in the first place...
Answer by Pradeep for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Android documents suggests to use getApplicationContext();
but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...
Ex:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
or
AlertDialog.Builder builder = new AlertDialog.Builder((Your Activity).this);
Answer by Satheesh for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Just change it into
AlertDialog.Builder alert_Categoryitem = new AlertDialog.Builder(YourActivity.this);
Instead of
AlertDialog.Builder alert_Categoryitem = new AlertDialog.Builder(getApplicationContext());
Answer by Metalhead1247 for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
You can also do this
public class Example extends Activity { final Context context = this; final Dialog dialog = new Dialog(context); }
This worked for me !!
Answer by mifthi for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
For nested dialogs this issue is very common, It works when
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(MyActivity.this);
is used instead of
mDialogBuilder = new AlertDialog.Builder(getApplicationContext);
this alternative.
Answer by Jinu for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Don't use getApplicationContext()
on declaring dialouge
Always use this
or your activity.this
Answer by mahbub_siddique for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Instead of getApplicationContext()
, just use ActivityName.this
Answer by Anubian Noob for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Another solution is to set the window type to a system dialog:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
This requires the SYSTEM_ALERT_WINDOW
permission:
As the docs say:
Very few applications should use this permission; these windows are intended for system-level interaction with the user.
This is a solution you should only use if you require a dialog that's not attached to an activity.
Answer by Suyog Gunjal for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
This Worked for me--
new AlertDialog.Builder(MainActivity.this) .setMessage(Html.fromHtml("Spread Knowledge Unto The Last")) .setCancelable(false) .setPositiveButton("Dismiss", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }).show();
Use
ActivityName.this
Answer by Faisal Ashraf for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
public class Splash extends Activity { Location location; LocationManager locationManager; LocationListener locationlistener; ImageView image_view; ublic static ProgressDialog progressdialog; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.splash); progressdialog = new ProgressDialog(Splash.this); image_view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub locationManager.requestLocationUpdates("gps", 100000, 1, locationlistener); Toast.makeText(getApplicationContext(), "Getting Location plz wait...", Toast.LENGTH_SHORT).show(); progressdialog.setMessage("getting Location"); progressdialog.show(); Intent intent = new Intent(Splash.this,Show_LatLng.class); // } }); }
Text here:-
use this for getting activity context for progressdialog progressdialog = new ProgressDialog(Splash.this); or progressdialog = new ProgressDialog(this); use this for getting application context for BroadcastListener not for progressdialog. progressdialog = new ProgressDialog(getApplicationContext()); progressdialog = new ProgressDialog(getBaseContext());
Answer by Hugo for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
As it's said, you need an Activity as context for the dialog, use "YourActivity.this" for a static context or check here for how to use a dynamic one in a safe mode
Answer by hopetribe for Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
try to reset dialog window's type to WindowManager.LayoutParams.TYPE_SYSTEM_ALERT: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Don't forget using the permission "android.permission.SYSTEM_ALERT_WINDOW"
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment