Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Professional Android 4 Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 October 4th, 2012, 09:15 AM
Authorized User
Points: 189, Level: 3
Points: 189, Level: 3 Points: 189, Level: 3 Points: 189, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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..
Reply With Quote
  #2 (permalink)  
Old October 4th, 2012, 11:07 AM
Authorized User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
Reply With Quote
  #3 (permalink)  
Old October 4th, 2012, 02:48 PM
Authorized User
Points: 189, Level: 3
Points: 189, Level: 3 Points: 189, Level: 3 Points: 189, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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..
Reply With Quote
  #4 (permalink)  
Old October 28th, 2012, 09:15 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #5 (permalink)  
Old October 28th, 2012, 09:36 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #6 (permalink)  
Old October 28th, 2012, 10:07 AM
Authorized User
Points: 189, Level: 3
Points: 189, Level: 3 Points: 189, Level: 3 Points: 189, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #7 (permalink)  
Old January 21st, 2013, 08:46 AM
Authorized User
Points: 89, Level: 1
Points: 89, Level: 1 Points: 89, Level: 1 Points: 89, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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..
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
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



All times are GMT -4. The time now is 09:53 PM.


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