[android-developers] Cannot handle exception in a thread

2011-06-14 Thread Droid
I am spinning a thread to go online and get data. But if phone is in a
network hole (no signal) it throws an exception.
In the exception I try to call Toast or finish() the activity but both
those cause a crash.

Here's the error and my code:
 error is:
06-14 11:32:01.691: ERROR/AndroidRuntime(7389):
java.lang.RuntimeException: Can't create handler inside thread that
has not called Looper.prepare()

here is the code, note the catch clause at the end which is where my
problem lies:

*
private Thread checkUpdate = new Thread() {
public void run() {
try {
URL updateURL = new URL(http://search.twitter.com/
search.json?q=+string_from_speaker);
URLConnection conn = updateURL.openConnection();
...
} catch (Exception e) {
// none of these exception handlers work becaue they are in wrong
thread!! WTF?
// NOPE tv1.setText( having trouble getting online); // TODO
// NOPE Toast.makeText(IconicAgain.this, having trouble getting
online + e, Toast.LENGTH_LONG).show();
}
}
};


Can I at least stop this thread somehow? Or close the activity
somehow?

-- 
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


Re: [android-developers] Cannot handle exception in a thread

2011-06-14 Thread Mark Murphy
Use runOnUiThread() around your code in the catch block, to have that
stuff be done on the main application thread.

On Tue, Jun 14, 2011 at 6:40 AM, Droid rod...@gmail.com wrote:
 I am spinning a thread to go online and get data. But if phone is in a
 network hole (no signal) it throws an exception.
 In the exception I try to call Toast or finish() the activity but both
 those cause a crash.

 Here's the error and my code:
  error is:
 06-14 11:32:01.691: ERROR/AndroidRuntime(7389):
 java.lang.RuntimeException: Can't create handler inside thread that
 has not called Looper.prepare()

 here is the code, note the catch clause at the end which is where my
 problem lies:

 *
    private Thread checkUpdate = new Thread() {
        public void run() {
            try {
                URL updateURL = new URL(http://search.twitter.com/
 search.json?q=+string_from_speaker);
                URLConnection conn = updateURL.openConnection();
                ...
            } catch (Exception e) {
 // none of these exception handlers work becaue they are in wrong
 thread!! WTF?
 // NOPE tv1.setText( having trouble getting online); // TODO
 // NOPE Toast.makeText(IconicAgain.this, having trouble getting
 online + e, Toast.LENGTH_LONG).show();
            }
        }
    };
 

 Can I at least stop this thread somehow? Or close the activity
 somehow?

 --
 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




-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in NYC: http://marakana.com/training/android/

-- 
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


Re: [android-developers] Cannot handle exception in a thread

2011-06-14 Thread Prakash Iyer
And when you return from the run method the thread will stop/die by itself.
Ideally your main Activity which started this thread off will be the one
notifying the user and if needed calling finish...

On Tue, Jun 14, 2011 at 7:10 AM, Mark Murphy mmur...@commonsware.comwrote:

 Use runOnUiThread() around your code in the catch block, to have that
 stuff be done on the main application thread.

 On Tue, Jun 14, 2011 at 6:40 AM, Droid rod...@gmail.com wrote:
  I am spinning a thread to go online and get data. But if phone is in a
  network hole (no signal) it throws an exception.
  In the exception I try to call Toast or finish() the activity but both
  those cause a crash.
 
  Here's the error and my code:
   error is:
  06-14 11:32:01.691: ERROR/AndroidRuntime(7389):
  java.lang.RuntimeException: Can't create handler inside thread that
  has not called Looper.prepare()
 
  here is the code, note the catch clause at the end which is where my
  problem lies:
 
 
 *
 private Thread checkUpdate = new Thread() {
 public void run() {
 try {
 URL updateURL = new URL(http://search.twitter.com/
  search.json?q=+string_from_speaker);
 URLConnection conn = updateURL.openConnection();
 ...
 } catch (Exception e) {
  // none of these exception handlers work becaue they are in wrong
  thread!! WTF?
  // NOPE tv1.setText( having trouble getting online); // TODO
  // NOPE Toast.makeText(IconicAgain.this, having trouble getting
  online + e, Toast.LENGTH_LONG).show();
 }
 }
 };
 
 
 
  Can I at least stop this thread somehow? Or close the activity
  somehow?
 
  --
  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
 



 --
 Mark Murphy (a Commons Guy)
 http://commonsware.com | http://github.com/commonsguy
 http://commonsware.com/blog | http://twitter.com/commonsguy

 Android Training in NYC: http://marakana.com/training/android/

 --
 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


-- 
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