Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Professional Android 2 Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 13th, 2010, 11:00 PM
Registered User
 
Join Date: Aug 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default CH 9 - Earthquake 2 (Async Task exmample) does not work

Has anyone got this working ? I am getting the following message in LogCat ...

Uncaught handler: thread earthquakeUpdates exiting due to uncaught exception
java.lang.ExceptionInInitializerError
at com.paad.earthquake.EarthquakeService.refreshEarth quakes(EarthquakeService.java:112)
at com.paad.earthquake.EarthquakeService.access$0(Ear thquakeService.java:109)
at com.paad.earthquake.EarthquakeService$1.run(Earthq uakeService.java:104)
at java.util.Timer$TimerImpl.run(Timer.java:290)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncT ask.java:421)
at android.os.AsyncTask$InternalHandler.<init>(AsyncT ask.java:421)
at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
... 4 more

Any idea ?
Reply With Quote
  #2 (permalink)  
Old November 2nd, 2010, 04:47 PM
Registered User
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just to chime in, I was having exactly the same issue. I did a little searching online and found some info on this. Apparently the actual task being run needs to be run in a android.os.Handler to work properly. I also found some other issues with the overall architecture when this is implemented properly.

Here are the changes I made, all within EarthquakeService:

Change:
Code:
  private TimerTask doRefresh = new TimerTask() {
    public void run() {
      refreshEarthquakes();
    }
  };
to:
Code:
        private Handler handler = new Handler();
	myTimerTask doRefresh = null;
	
	public class myTimerTask extends TimerTask {
		private Runnable runnable = new Runnable() {
			public void run() {
				try {
					refreshEarthquakes();
				} catch(Exception e) {
					Log.e(">>>> Error executing refreshEarthquakes: " , 
						e.getMessage());
				}
			}
		};
		
		public void run() {
			handler.post(runnable);
		}
	}
In onStartCommand(), change:
Code:
    updateTimer.cancel();
    if (autoUpdate) {
      updateTimer = new Timer("earthquakeUpdates");
      updateTimer.scheduleAtFixedRate(doRefresh, 0, updateFreq*60*1000);
    }
to:
Code:
		if (doRefresh != null) {
			doRefresh.cancel();
		}
		doRefresh = new myTimerTask();
		updateTimer.cancel();
		if (autoUpdate) {
			updateTimer = new Timer("earthquakeUpdates");
			updateTimer.scheduleAtFixedRate(doRefresh , 0 , 
				updateFreq * 60 * 1000);
The basic change is adding the Handler.post() call. I found that if you don't call cancel() on the Timer and TimerTask explicitly, an exception will be thrown. It seems odd to me that the Timer class doesn't cancel it's TimerTasks when you can cancel() on it considering that, according to the documentation here http://developer.android.com/referen...l#cancel%28%29, it should.

If someone knows of a cleaner, more efficient way of doing this, I'm all ears. This is the type of thing that makes me love using Python as this seems so overly complicated for what you are actually doing, unless there is a better way of doing this...
Reply With Quote
  #3 (permalink)  
Old November 2nd, 2010, 08:40 PM
Registered User
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, another error I found while debugging this example is that the BroadcastReceiver in EarthquakeMap should be unregistered in onPause(), like it is in the main Earthquake Activity. So, make sure you add the following to the onPause() method in EarthquakeMap before you call super.onPause():
Code:
unregisterReceiver(receiver);
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 7 Earthquake 2, Earthquake.java, error onActivityResult dbomberg BOOK: Professional Android 2 Application Development 2 November 30th, 2012 05:52 AM
Chap. 9 Earthquake app does not work. pgmr26 BOOK: Professional Android 2 Application Development 1 March 27th, 2010 08:14 AM
Example in CH 4 doesn't work in SeaMonkey browser MarkEHansen BOOK: Professional ASP.NET 2.0 Design: CSS, Themes, and Master Pages ISBN: 978-0-470-12448-2 2 November 28th, 2008 05:21 PM
can't get messagebox example in ch 18 to work itlotl VB.NET 2002/2003 Basics 3 January 13th, 2006 04:39 PM
Ch 3 Examples Don't Work Gregorio BOOK: Professional Crystal Reports for VS.NET 3 February 15th, 2005 11:36 AM



All times are GMT -4. The time now is 02:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.