Re: [android-developers] Re: WrongThread exception - how to analyze/debug?

2014-05-23 Thread plnelson


On Wednesday, May 21, 2014 12:23:32 PM UTC-4, Kostya Vasilyev wrote:



 Is there more to the message? I grepped the base framework, and came up 
 with these:


The entire message is CalledFromWrongThreadException: Only the original 
thread that created a view hierarchy can touch its views 

And it's widely discussed on places like Stack Overflow but the focus of 
the dicscussions is always on fixing the bug, whereas I'm posting this to 
the Google group instead of Stack Overflo0w because I want to *understand*what 
it actually 
*means*.   What do they mean by the wrong thread?   If the same thread 
(based on the debugger and trace) is accessing it when it generates the 
exception and when it doesn't, how does that thread become the wrong 
thread?   Where does Google document its exceptions?  

*And what does the Eclipse debugger thread nomenclature ( Thread x[ 
Thread-y] ) mean?*

 The Eclipse debugger seems to refer to a thread 9 and a thread 10.   What 
 does the debugger mean when it displays it that way?


 They're just thread ids (the x) and auto-generated thread names.


Yes, I know, but why are there *two* of them on one line?  That's my 
question.  Normally I see one TID per line, so what is Eclipse telling us 
when it uses *Thread x[ Thread-y] syntax?*




-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [android-developers] Re: WrongThread exception - how to analyze/debug?

2014-05-23 Thread Kostya Vasilyev
2014-05-24 0:05 GMT+04:00 plnelson pna...@gmail.com:



 On Wednesday, May 21, 2014 12:23:32 PM UTC-4, Kostya Vasilyev wrote:



 Is there more to the message? I grepped the base framework, and came up
 with these:


 The entire message is CalledFromWrongThreadException: Only the original
 thread that created a view hierarchy can touch its views

 And it's widely discussed on places like Stack Overflow but the focus of
 the dicscussions is always on fixing the bug,


Fixing the bug should ultimately be your goal, no?


 whereas I'm posting this to the Google group instead of Stack Overflo0w
 because I want to *understand* what it actually *means*.   What do they
 mean by the wrong thread?


The view hierarchy should only be touched from the UI thread that's
created for you by Android (labeled main thread in Eclipse).

Now, some view methods may have checks for this, and some may not (in fact,
the vast majority does not).


 If the same thread (based on the debugger and trace) is accessing it when
 it generates the exception and when it doesn't, how does that thread become
 the wrong thread?   Where does Google document its exceptions?


http://developer.android.com/guide/components/processes-and-threads.html

Additionally, the Andoid UI toolkit is *not* thread-safe. So, you must not
manipulate your UI from a worker thread—you must do all manipulation to
your user interface from the UI thread. Thus, there are simply two rules to
Android's single thread model:

   1. Do not block the UI thread
   2. Do not access the Android UI toolkit from outside the UI thread




 *And what does the Eclipse debugger thread nomenclature ( Thread x[
 Thread-y] ) mean?*

 The Eclipse debugger seems to refer to a thread 9 and a thread 10.   What
 does the debugger mean when it displays it that way?


 They're just thread ids (the x) and auto-generated thread names.


 Yes, I know, but why are there *two* of them on one line?  That's my
 question.  Normally I see one TID per line, so what is Eclipse telling us
 when it uses *Thread x[ Thread-y] syntax?*


I really don't know. Looks like the name is assigned based on thread id,
but does not use the thread id directly -- maybe some little piece of code
with own counter, etc.

But if this discussion is to be focused on only the original thread can
touch the view hierarchy -- you should not be touching views from any
thread other than the main UI thread, and that one is clearly labeled in
Eclipse as Thread [1 main].

Hope I didn't make it completely nonsensical.

-- K

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[android-developers] Re: WrongThread exception - how to analyze/debug?

2014-05-21 Thread plnelson
On Tuesday, May 20, 2014 11:51:35 PM UTC-4, Jonathan S wrote:

 Can you copy and paste code in the *MyRemoteActivity class?*


 It's 700 lines of (mostly proprietary) code, so no I don't think that 
would be practical.Anyway,* my questions are generic Android questions*; 
they're not specific to the code:   I'm not asking for anyone to debug this 
for me; I'm just trying to understand more so I can debug it myself.

1. * If it's the wrong thread then why does the stack trace show the same 
thread in the cases where it crashes and where it doesn't?* 
...in other words what does Android mean by wrong thread?  If the 
debugger shows it executing in the same thread when it's not crashing as 
when it lands in the catch block with the wrong thread then what exactly 
do they mean by wrong thread?

*And what does the Eclipse debugger thread nomenclature ( Thread x[ 
Thread-y] ) mean?*
The Eclipse debugger seems to refer to a thread 9 and a thread 10.   What 
does the debugger mean when it displays it that way?

*What are some good strategies for analyzing wrong thread bugs? *  
Just that -  what tools or strategies are recommended to see what code is 
executing in which threads - how do experienced Android programmers debug 
these kinds of exceptions?

 

 On Tuesday, May 20, 2014 4:17:16 PM UTC-4, plnelson wrote:

 I have a multi-threaded Android app. One thread wakes up periodically on 
 a timer and exchanges information over WiFi with a remote device and the 
 other one is the main thread that handles all the usual View stuff.

 Lately I've been getting a Called from Wrong Thread Exception in a 
 catch block in the remote handler thread.

 If I set a breakpoint in the remote handler code, when it's NOT crashing, 
 the stack trace in the Eclipse debugger says

 *Thread[ 9 Thread-10] (Suspended breakpoint at line 600 in 
 MyRemoteActivity)*

 But if I set a breakpoint in my catch-block when it crashes it names the 
 same threads, i.e.,

 *Thread[ 9 Thread-10] (Suspended breakpoint at line 605 in 
 MyRemoteActivity)*

 (... different line number because it's the catch-block which is a few 
 lines down.)

 My question: If it's the wrong thread then why does the stack trace show 
 the same thread in the cases where it crashes and where it doesn't? And 
 what does the Eclipse debugger thread nomenclature ( Thread x[ Thread-y] 
 ) mean? What are some good strategies for analyzing wrong thread bugs?   
 
 Thanks in advance!




-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [android-developers] Re: WrongThread exception - how to analyze/debug?

2014-05-21 Thread Kostya Vasilyev
2014-05-21 20:02 GMT+04:00 plnelson pna...@gmail.com:

 On Tuesday, May 20, 2014 11:51:35 PM UTC-4, Jonathan S wrote:

 Can you copy and paste code in the *MyRemoteActivity class?*


 It's 700 lines of (mostly proprietary) code, so no I don't think that
 would be practical.Anyway,* my questions are generic Android
 questions*; they're not specific to the code:   I'm not asking for anyone
 to debug this for me; I'm just trying to understand more so I can debug it
 myself.

 1. * If it's the wrong thread then why does the stack trace show the same
 thread in the cases where it crashes and where it doesn't?*
 ...in other words what does Android mean by wrong thread?  If the
 debugger shows it executing in the same thread when it's not crashing as
 when it lands in the catch block with the wrong thread then what exactly
 do they mean by wrong thread?


Is there more to the message? I grepped the base framework, and came up
with these:

./core/tests/coretests/src/android/widget/focus/RequestFocusTest.java:
 // Test that a requestFocus from the wrong thread fails.
./core/tests/coretests/src/android/widget/focus/RequestFocusTest.java:
   fail(requestFocus from wrong thread should raise exception.);
./opengl/java/android/opengl/ManagedEGLContext.java:throw new
IllegalStateException(Called on wrong thread);
./opengl/java/android/opengl/GLErrorWrapper.java:
 OpenGL method called from wrong thread.);

Two of these are in tests.




 *And what does the Eclipse debugger thread nomenclature ( Thread x[
 Thread-y] ) mean?*
 The Eclipse debugger seems to refer to a thread 9 and a thread 10.   What
 does the debugger mean when it displays it that way?


They're just thread ids (the x) and auto-generated thread names.

When you debug in Eclipse, you should see other threads with more
meaningful names, e.g. main, Binder_x, etc.

You can assign names to your threads by calling Thread.setName. Personally,
I find it very useful for debugging and postmortem crash reports:

http://developer.android.com/reference/java/lang/Thread.html#setName(java.lang.String)




 *What are some good strategies for analyzing wrong thread bugs? *
 Just that -  what tools or strategies are recommended to see what code is
 executing in which threads - how do experienced Android programmers debug
 these kinds of exceptions?


Not sure I qualify as an experienced Android programmer, but...

...

From your original description (I snipped it, it was below), it seems you
have some code that runs on a worker thread, and yet is placed in a file
called MyRemoteActivity.

There is nothing wrong with naming source files anything you wish, but ---
is that actually an Android activity?

Are you sure the code in that file, that runs on a non-main thread, does
not make any calls to Android UI code?

Conversely, your code should not be making networking calls on the main
thread. You can use StrictMode to log such events:

http://developer.android.com/reference/android/os/StrictMode.html

And going back to your original message again:

the other one is the main thread that handles all the usual View stuff

Do you mean the main Android thread that get pre-created for you, or do
you mean there is a different thread that your code creates (and which
you're calling main) that interacts with the Android UI framework?

You should only call Android UI framework methods from the main thread
that gets pre-created for you by Android, that's the thread on which all
component callbacks are called (onCreate, etc.).

-- K

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[android-developers] Re: WrongThread exception - how to analyze/debug?

2014-05-20 Thread Jonathan S
Any class that touch View MUST BE in the Main Thread only.

On Tuesday, May 20, 2014 4:17:16 PM UTC-4, plnelson wrote:

 I have a multi-threaded Android app. One thread wakes up periodically on a 
 timer and exchanges information over WiFi with a remote device and the 
 other one is the main thread that handles all the usual View stuff.

 Lately I've been getting a Called from Wrong Thread Exception in a catch 
 block in the remote handler thread.

 If I set a breakpoint in the remote handler code, when it's NOT crashing, 
 the stack trace in the Eclipse debugger says

 *Thread[ 9 Thread-10] (Suspended breakpoint at line 600 in 
 MyRemoteActivity)*

 But if I set a breakpoint in my catch-block when it crashes it names the 
 same threads, i.e.,

 *Thread[ 9 Thread-10] (Suspended breakpoint at line 605 in 
 MyRemoteActivity)*

 (... different line number because it's the catch-block which is a few 
 lines down.)

 My question: If it's the wrong thread then why does the stack trace show 
 the same thread in the cases where it crashes and where it doesn't? And 
 what does the Eclipse debugger thread nomenclature ( Thread x[ Thread-y] 
 ) mean? What are some good strategies for analyzing wrong thread bugs?   
 
 Thanks in advance!




-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[android-developers] Re: WrongThread exception - how to analyze/debug?

2014-05-20 Thread Jonathan S
Can you copy and paste code in the *MyRemoteActivity class?*

On Tuesday, May 20, 2014 4:17:16 PM UTC-4, plnelson wrote:

 I have a multi-threaded Android app. One thread wakes up periodically on a 
 timer and exchanges information over WiFi with a remote device and the 
 other one is the main thread that handles all the usual View stuff.

 Lately I've been getting a Called from Wrong Thread Exception in a catch 
 block in the remote handler thread.

 If I set a breakpoint in the remote handler code, when it's NOT crashing, 
 the stack trace in the Eclipse debugger says

 *Thread[ 9 Thread-10] (Suspended breakpoint at line 600 in 
 MyRemoteActivity)*

 But if I set a breakpoint in my catch-block when it crashes it names the 
 same threads, i.e.,

 *Thread[ 9 Thread-10] (Suspended breakpoint at line 605 in 
 MyRemoteActivity)*

 (... different line number because it's the catch-block which is a few 
 lines down.)

 My question: If it's the wrong thread then why does the stack trace show 
 the same thread in the cases where it crashes and where it doesn't? And 
 what does the Eclipse debugger thread nomenclature ( Thread x[ Thread-y] 
 ) mean? What are some good strategies for analyzing wrong thread bugs?   
 
 Thanks in advance!




-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.