Wrox Programmer Forums
|
BOOK: Professional Android 4 Application Development
This is the forum to discuss the Wrox book Professional Android 4 Application Development Reto Meier; ISBN: 978-1-1181-0227-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2012, 09:15 AM
Authorized User
 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 2 Times in 2 Posts
Default Chap 6 Earthquake new thread problem

In the Ch6 Earthquake program there's a problem with initiating an internet access from a fragment (EarthquakeListFragment.java)

Specifically, the author says that due to API changes and best practices instead of using a direct call to the xml retrieve/parse routine as in:

Code:
public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		int layoutID = android.R.layout.simple_list_item_1;
		aa = new ArrayAdapter<Quake>(getActivity(), layoutID , earthquakes);
		setListAdapter(aa);

refreshEarthquakes();

You should replace the single line of refreshEarthquakes() with the following. It should open a new thread to call the routine and keep the processing off the main thread:

Code:
Thread t = new Thread(new Runnable() {
			public void run() {
				refreshEarthquakes();
			}
		});
		
		t.start();

The problem is twofold.

1) The new thread code he offered doesn't seem to work correctly.

2) If i remove the <uses-sdk ...../> clause from the manifest file the original version (#1 above) works, but including the clause with either version causes the application to crash.

Is there a quick solution that anyone has found to get this to work, or must it use some type of Async Task or service?

thanks

Last edited by xcopy; October 4th, 2012 at 09:18 AM..
 
Old October 4th, 2012, 11:07 AM
Authorized User
 
Join Date: Oct 2012
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What do you mean by "doesn't seem to work correctly."? Is there an exception being thrown?
 
Old October 4th, 2012, 02:48 PM
Authorized User
 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Apologize for my noobness and sorry if i didn't provide all information.

The array adapter didn't like being modified by this thread because I omitted the handler/runnable wrapper. Once I figured out my complete brain fade and saw that it was missing, the app runs as it should.

Code:
	handler.post(new Runnable() {
			public void run() {
			    addNewQuake(quake);
			}
		});

Still, I find it strange that the manifestation of this problem was in the manifest file. Why the uses-sdk clause caused the app to fail, or run when it was removed, is odd. To me anyway..

Last edited by xcopy; October 4th, 2012 at 05:00 PM..
 
Old October 28th, 2012, 09:15 AM
Registered User
 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Same problem

Hi xcopy,

I am a noob and am having the same problem. Can you please explain in more details how you solve the problem? Thanks in advance.
 
Old October 28th, 2012, 09:36 AM
Registered User
 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem solved

After scratching my head, I realised the error. The code is provided is not correct and please follow the code in the book for EarthquakeListFragment.java.
 
Old October 28th, 2012, 10:07 AM
Authorized User
 
Join Date: Jul 2012
Posts: 11
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by chod View Post
After scratching my head, I realised the error. The code is provided is not correct and please follow the code in the book for EarthquakeListFragment.java.
Exactly. I guess it should show up on an errata or something, but the book is correct and the downloaded code is wrong.
 
Old January 21st, 2013, 08:46 AM
Authorized User
 
Join Date: Jan 2013
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter_6_Earthquake

A possible "quick" solution i posted to the problem that newbie had (no earthquake listing was generated).
I found it in my environment due to exception : "android.view.ViewRoot$CalledFromWrongThreadExcept ion: Only the original thread that created a view hierarchy can touch its views"
when aa.notifyDataSetChanged() is called.

Hope this can help..





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
problem with earthquake chapter 7 red78 BOOK: Professional Android 2 Application Development 0 August 8th, 2012 04:50 PM
Problem with earthquake viewer takhisis1 BOOK: Professional Android 2 Application Development 1 June 25th, 2012 09:41 AM
Chap7-2 Earthquake sort problem dbald1995 BOOK: Professional Android 2 Application Development 0 March 5th, 2012 10:50 PM
Chap. 9 Earthquake app does not work. pgmr26 BOOK: Professional Android 2 Application Development 1 March 27th, 2010 08:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.