0

I am displaying a toast outside of the Application.

I works fine like this with Toast:

  Toast.makeText(getBaseContext(),"Text").show();

But when I try with Dialog i returns a

        final Dialog dialog_edu = new Dialog(getBaseContext());
        dialog_edu.setTitle("Choose Action");       
        dialog_edu.show();

it returns this:

 01-28 18:02:04.734: W/WindowManager(329): Attempted to add window with non-application token WindowToken{41c01dd8 token=null}.  Aborting.
 01-28 18:02:04.734: E/AndroidRuntime(2626): FATAL EXCEPTION: main
 01-28 18:02:04.734: E/AndroidRuntime(2626): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

If I use "MyAcitivity.this" for the context it works within the application, but not when i go out for the application. The Toast on the other hand works inside and outside of the application.

I have read, that you can use : getActivity() and others, but in the current state it returns the same error.

If I use a handler and try to delay the build of the dialog, it has no effect for a delay period of 1 full second.

So how do i get a Dialog to work inside and outside of the application.

mmBs
  • 8,421
  • 6
  • 38
  • 46
Niels
  • 635
  • 3
  • 9
  • 23
  • possible duplicate of [Android: ProgressDialog.show() crashes with getApplicationContext](http://stackoverflow.com/questions/1561803/android-progressdialog-show-crashes-with-getapplicationcontext) – GhostDerfel Jan 28 '14 at 17:09
  • I would say no - I have read that Thread. His question is much more simple - I am trying to get a dialog to show outside of the application. As well all the stuff that people suggested in there makes no difference on the error i get in LogCat. – Niels Jan 28 '14 at 17:14
  • 1
    You can't show a dialog from somewhere that has no window: http://www.doubleencore.com/2013/06/context/ – zapl Jan 28 '14 at 17:20
  • @zapl could you please explain in context with my problem, how to go on from my state ? – Niels Jan 28 '14 at 17:31
  • If you are within `Application` or `Service` and you want to show a dialog you would have to start an Activity that shows the dialog. It's on the other hand not nice since you can interrupt a user that way because the activity just shows even if the user has already left your app and is doing something else, e.g. incoming phonecall. – zapl Jan 28 '14 at 17:42
  • I am calling the Toast with a SensorEventListener.. If the listener is activated the Toast appears.. I would though like to display a Dialog instead of the Toast activated by the SensorEventListener. – Niels Jan 28 '14 at 17:56

3 Answers3

0

You are probably missing (). So it would be like,

final Dialog dialog_edu = new Dialog(getBaseContext());
Bipin Bhandari
  • 2,694
  • 23
  • 38
0

Use the getParent() instead of getBaseContext().

otherwise, use like below. because this issue is an timing issue.

Context context = getBaseContext(); new Dialog(context);

Try to above two ways. :-)

allsoft
  • 198
  • 1
  • 10
0

So how do i get a Dialog to work inside and outside of the application.

You create a Dialog-themed Activity.

You cannot show a standard Dialog outside of an Activity with any old Context. Although the dialog constructor requires just a Context, it actually requires an Activity, presumably for attaching itself to the Activity's window. Toast does not have this limitation, so you can use any Context.

dominicoder
  • 9,338
  • 1
  • 26
  • 32