I've not been able to come up with a single use case for calling
getApplicationContext(); I suspect the need for it is ENTIRELY
internal. Also getBaseContext().

Generally, you want to pass along the most specific context available.
That will be your current activity or service. UI components like
Views always need an Activity as a context, as a practical matter.

As long as I'm explaining that point, let me go on to cover the rest
of the topic -- the Application as a context:

If you're looking for a shared context (as in sharing a common object
between activities), that would be getApplication()..

getApplicationContext() can return you an entirely different
application from the same process. You can't access anything
interesting in it -- it was loaded with a different classloader, and
you don't really have any control over just WHICH application object
will be returned. It's best to just consider it a toxic object you
don't want to be touching, because it can only add bugs, not
functionality.

It's tempting, or at least it was to me until I learned better, to
override getApplicationContext() to typecast to the more specific
application class. But that doesn't work for the above reasons. And it
doesn't work to do it for getApplication(), since that is declared
final.

So I have a getApp() method in a common activity base class that casts
to my specific app class.

You have to be careful with Application objects anyway -- they can't
own any dynamic state. An activity or service must always take
responsibility for persisting any dynamic state.


On Mar 15, 2:47 pm, Dirk Vranckaert <dirkvrancka...@gmail.com> wrote:
> Thx a lot, this works!
>
> On 15 mrt, 22:15, Adrian Vintu <adrianvi...@gmail.com> wrote:
>
>
>
> > This one hs been answered a couple of times on this forum.
>
> > Use
> > dialog = new Dialog(this);
> > instead of
> > dialog = new Dialog(getApplicationContext());
>
> > BR,
> > Adrian Vintu
>
> >http://adrianvintu.com
>
> > On Mon, Mar 15, 2010 at 10:05 PM, Dirk Vranckaert
> > <dirkvrancka...@gmail.com>wrote:
>
> > > Thx for the quick response but doesn't change a thing, it gives me
> > > exactly the same exception!
>
> > > On 15 mrt, 21:58, Kumar Bibek <coomar....@gmail.com> wrote:
> > > > Try getBaseContext();
>
> > > > Thanks and Regards,
> > > > Kumar Bibek
>
> > > > On Mar 16, 1:50 am, Dirk Vranckaert <dirkvrancka...@gmail.com> wrote:
>
> > > > > I'm trying to create a custom dialog in my application to show an
> > > > > about window but it ain't working. Maybe one of you knows a solution?
>
> > > > > So I have an activity with the onCreateDialog(int id) overriden in it:
>
> > > > >         @Override
> > > > >         protected Dialog onCreateDialog(int id) {
> > > > >                 Dialog dialog = null;
> > > > >                 switch (id) {
> > > > >                         case EPISODE_LOADING_DIALOG:
> > > > >                                 ...
> > > > >                         case EXCEPTION_DIALOG:
> > > > >                                 ...
> > > > >                         case ABOUT_DIALOG:
> > > > >                                 dialog = new
> > > Dialog(getApplicationContext());
>
> > > dialog.setContentView(R.layout.aboutdialog);
> > > > >                                 dialog.setTitle("MyTitle");
> > > > >                                 break;
> > > > >                         default:
> > > > >                                 ...
> > > > >                 }
> > > > >                 return dialog;
> > > > >         }
>
> > > > > My aboutdialog layout files looks like this:
>
> > > > > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> > > > > android"
> > > > >               android:orientation="vertical"
> > > > >               android:layout_width="fill_parent"
> > > > >               android:layout_height="fill_parent"
> > > > >               android:padding="10dp"
>
> > > > >     <TextView android:id="@+id/aboutText"
> > > > >               android:layout_width="wrap_content"
> > > > >               android:layout_height="fill_parent"
> > > > >               android:textColor="#FFF"
> > > > >               android:text="@string/aboutText"
> > > > >               />
> > > > > </LinearLayout>
>
> > > > > This is exactly as described here:
> > >http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
> > > > > However this my exception thrown:
>
> > > > > 03-15 21:48:32.055: ERROR/AndroidRuntime(2004): Uncaught handler:
> > > > > thread main exiting due to uncaught exception
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):
> > > > > android.view.WindowManager$BadTokenException: Unable to add window --
> > > > > token null is not for an application
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.ViewRoot.setView(ViewRoot.java:429)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.WindowManagerImpl.addView(WindowManagerImpl.java:178)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.app.Dialog.show(Dialog.java:231)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.app.Activity.showDialog(Activity.java:2407)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
>
> > > eu.vranckaert.episodeWatcher.EpisodesWatchListActivity.onOptionsItemSelecte
> > >  d(EpisodesWatchListActivity.java:
> > > > > 130)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.app.Activity.onMenuItemSelected(Activity.java:2085)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
>
> > > com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow
> > >  .java:
> > > > > 820)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:
> > > > > 139)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
>
> > > com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.ja
> > >  va:
> > > > > 813)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
>
> > > com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:
> > > > > 519)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
>
> > > com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemVi
> > >  ew.java:
> > > > > 122)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.View.onTouchEvent(View.java:3828)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.widget.TextView.onTouchEvent(TextView.java:6291)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.View.dispatchTouchEvent(View.java:3368)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > com.android.internal.policy.impl.PhoneWindow
> > > > > $DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.os.Handler.dispatchMessage(Handler.java:99)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.os.Looper.loop(Looper.java:123)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > android.app.ActivityThread.main(ActivityThread.java:3948)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > java.lang.reflect.Method.invokeNative(Native Method)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > java.lang.reflect.Method.invoke(Method.java:521)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > com.android.internal.os.ZygoteInit
> > > > > $MethodAndArgsCaller.run(ZygoteInit.java:782)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
> > > > > 03-15 21:48:32.130: ERROR/AndroidRuntime(2004):     at
> > > > > dalvik.system.NativeStart.main(Native Method)
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Developers" group.
> > > To post to this group, send email to android-developers@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > android-developers+unsubscr...@googlegroups.com<android-developers%2Bunsubs
> > >  cr...@googlegroups.com>
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to