The Looper is only created when your thread is alive.

When you call ' sharedPrioThread.start();', your sharedPrioThread is not 
immediately alive. 
Only when your sharedPrioThread's 'run()' method is called, then your 
thread is alive.

In other words, when you call 'sharedPrioThread.getLooper()' it may or may 
not return a Looper, depending on the fact whether its 'run()' method had 
been called by the Thread of sharedPrioThread itself:

sharedPrioThread.start():
At some point in time (soon but not immediately), the sharedPrioThread's 
'run()' method is called by its background Thread.

sharedPrioThread.getLooper():
If 'run()' has not yet been called, it will return null. If 'run()' has 
been called, it will return a Looper. 


On Tuesday, May 29, 2012 5:44:34 PM UTC-4, Panam wrote:
>
> In my app, I use a shared HandlerThread like this:
>
> public static synchronized HandlerThread getSharedPriorityThread(){
> if(sharedPrioThread == null){
> sharedPrioThread = new HandlerThread("sharedPrioThread", 
> android.os.Process.THREAD_PRIORITY_DISPLAY){
> @Override
> public boolean quit() {
> throw new NullPointerException("tried to quit sharedPrioThread looper!");
> }
> };
> sharedPrioThread.setDaemon(true);
> sharedPrioThread.start();
> }
> if(sharedPrioThread == null){
> throw new NullPointerException("sharedPrioThread is null!");
> }
> if(sharedPrioThread.getLooper() == null){
> throw new NullPointerException("sharedPrioThread looper is null");
> }
> return sharedPrioThread;
> }
>
> quit() is overriden in order to intercept any hidden calls to quit() on 
> the looper.
> In addition, ACRA is configured to intercept all Exceptions and send them 
> to a GoogleDocs spreadsheet, so I am sure I would not miss any.
>
> From time to time (when the app has run for some time), I get this 
> reported by ACRA on a real device (no exceptions before this one):
>
> "java.lang.RuntimeException: Unable to start activity ...
> Caused by: java.lang.NullPointerException: sharedWorkerThread looper is 
> null
>  at com.myapp.Utils.getSharedWorkerThread(Utils.java:112)
>  ...
> at 
> android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
> at android.app.Activity.performStart(Activity.java:3791)
> at 
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
> ... 11 more
>
> How can this happen (i.e. getLooper() suddenly returns null withouth 
> quit() being called on the looper before)?
> As far as I understand this, the looper must have been exited as the 
> threads isAlive() method returns false. Should this be possible at all in a 
> looper?
> I am using this HandlerThread for multiple Handlers (which might be 
> nullified from time to time) as well as its looper for a locationListener.
> Is this a problem?  Is it a problem because it is a daemon thread?
>
> Thanks!
>

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

Reply via email to