Dirk,
You cannot create UI elements in threads that don't have a message-
looper themselves.
I usually do this.
Call the 'post' (or 'postDelayed') method.
In your example, I assume that the method getEpisodes() is part of a
Thread instances that is defined as a non-static inner (anonymous)
class of your activity. This means that you can call 'post' inside
your getEpisodes() method:
private void getEpisodes() {
try {
episodes = myEpisodesService.retrieveEpisodes
(user);
} catch (InternetConnectivityException e) {
String message = "Could not connect to host";
Log.e(LOG_TAG, message, e);
exceptionMessageResId =
R.string.internetConnectionFailureReload;
post(new Runnable() {
public void run() {
showDialog(EXCEPTION_DIALOG);
}
});
}
runOnUiThread(returnEpisodes);
}
If your Thread is not an inner class of your Activity and you
therefore can't call 'post', give your thread a reference to your
activity:
private void getEpisodes() {
try {
episodes = myEpisodesService.retrieveEpisodes
(user);
} catch (InternetConnectivityException e) {
String message = "Could not connect to host";
Log.e(LOG_TAG, message, e);
exceptionMessageResId =
R.string.internetConnectionFailureReload;
mMyActivity.post(new Runnable() {
public void run() {
showDialog(EXCEPTION_DIALOG);
}
});
}
runOnUiThread(returnEpisodes);
}
On Jan 21, 4:30 pm, Dirk Vranckaert <[email protected]> wrote:
> It seems your solution does not work either. As long as I'm doing it
> in a thread it doesn't work.
>
> Altough what I tried next was setting the error message for the user
> in my catch clause. And right after the thread finished I handled the
> showing of the errors based on wheater some error message was set or
> not!
>
> Thx for your help! (If you get me some advanced explanation on why
> dialogs cannot be created from within a thread plz let me know!)
>
> On 21 jan, 20:47, Dirk Vranckaert <[email protected]> wrote:
>
>
>
> > I certainly will try it out later this evening (studying for my
> > certification right now) but is there any more detailed/advanced
> > explanation about why I can't run a bunch of code in a thread and at
> > the end of the thread start a dialog? Why I have to start a nested
> > thread with just the dialog creation? :s Still don't really get the
> > reason why...
>
> > Anyway thx in advance for giving me a possible working solution, and
> > I'll certainly keep you informed if it did the job for me (later this
> > evening).
>
> > On 21 jan, 19:47, TreKing <[email protected]> wrote:
>
> > > On Thu, Jan 21, 2010 at 12:28 PM, Dirk Vranckaert
> > > <[email protected]>wrote:
>
> > > > So if I understand you correct I should run two nested threads to show
> > > > the dialog?
>
> > > Yup. So instead of showDialog(int) in the thread, do
> > > runOnUiThread(someThread); where someThread just calls showDialog(int) in
> > > it's run method.
>
> > > Yes, it's annoying and messy, but you can't do UI related operations in
> > > separate threads, so there you go.
>
> > > -------------------------------------------------------------------------------------------------
> > > TreKing - Chicago transit tracking app for Android-powered
> > > deviceshttp://sites.google.com/site/rezmobileapps/treking- Hide quoted
> > > text -
>
> - Show quoted text -
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en