Wrox Programmer Forums
| 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 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
  #21 (permalink)  
Old September 25th, 2010, 06:04 AM
Registered User
 
Join Date: Sep 2010
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Just want to point out that the solution to this is as described by jadoti in posts 15 and 16.

Another thing worth noticing, is that after these changes to use the built-in XML preferences, the preferences activity no longer return a result Activity.RESULT_OK (there is no OK or CANCEL button, you get back to the main activity by using the back-button). It is therefore necessary to change the on ActivityResult function if you want the quake list to update after changing the preferences:

Code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	
	if (requestCode == SHOW_PREFERENCES) {
		//if (resultCode == Activity.RESULT_OK) {
			updateFromPreferences();
			refreshEarthquakes();
		//}
	}
}
Notice the commented-out check for the result code. They should be removed.

Of course, this will probably not be necessary when we learn about adding listeners to preference changes. But for now at least, this is needed if you want your list update.

Last edited by Bofur; September 25th, 2010 at 06:10 AM..
  #22 (permalink)  
Old January 18th, 2011, 11:45 AM
Registered User
 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Is this bug already fixed in the Code Download-Example Projects?

Hi I'm new to Android development and I'm having an issue with Earthquake sample . It worked fine until chapter 6, but after that I get an error saying "The application Earthquake has stopped unexpectedly", even if I try with the sample code for the following chapters I get same error. I've being trying to apply the fixes described in this thread but the problems persists.
  #23 (permalink)  
Old January 26th, 2011, 02:07 PM
Registered User
 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This issue is mainly related to this:

http://code.google.com/p/android/issues/detail?id=2096

In order to solve it, some things must be done, first, only use string-array in your resource arrays.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="update_freq_options">
		<item>Every Minute</item>
		<item>5 minutes</item>
		<item>10 minutes</item>
		<item>15 minutes</item>
		<item>Every Hour</item>
	</string-array>
	<string-array name="magnitudes">
		<item>3</item>
		<item>5</item>
		<item>6</item>
		<item>7</item>
		<item>8</item>
	</string-array>
	<string-array name="magnitude_options">
		<item>3</item>
		<item>5</item>
		<item>6</item>
		<item>7</item>
		<item>8</item>
	</string-array>
	<string-array name="update_freq_value">
		<item>1</item>
		<item>5</item>
		<item>10</item>
		<item>15</item>
		<item>60</item>
	</string-array>
</resources>
In the Activity Earthquake the method updateFromPreferences should be like this, because using the standard preferences the value selected will be directly returned (and not the index for the array).

Code:
private void updateFromPreferences() {
		Context context = getApplicationContext();
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		
		minimumMagnitude = Integer.parseInt(prefs.getString(Preferences.PREF_MIN_MAG, "0"));
		updateFreq = Integer.parseInt(prefs.getString(Preferences.PREF_UPDATE_FREQ, "0"));
		
		autoUpdate = prefs.getBoolean(Preferences.PREF_AUTO_UPDATE, false);
	}
Another important change is to delete the requestCode check in the onActivityResult method, because we are returning from the preferences screen using the back functionality and this will return us a RESULT_CANCELED.

Code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == SHOW_PREFERENCES)
			//if (resultCode == Activity.RESULT_OK) {
				updateFromPreferences();
				refreshEarthquakes();
			//}
	}
Applying all this changes the sample will work perfectly.

Only one last issue, if you have already stored preferences as integers the code will fail because they must be cleaned, I am still looking for the way to clean the preferences without having to add a little trick in the code. Just clear the preferences and then commit.

Last edited by polilla; January 26th, 2011 at 02:12 PM..
  #24 (permalink)  
Old April 4th, 2011, 07:05 PM
Registered User
 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

I just want to say thanks to everyone who contributed in this thread. It was very frustrating to deal with this section, since the problem isn't very clear, the code in the book isn't right, and the "Errata" section on the wrox website doesn't even mention this.
  #25 (permalink)  
Old December 5th, 2011, 07:47 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i've inputted the code for the earthquakes part in chapter 6 up to p197 and the app crashes when i select a number of magnitude greater than 5 and it also doesn't update the page and leaves the previous in place.

So the code needs changing quite significantly.

Cheers

P.S. the book seems to have quite a few errors to be honest.
  #26 (permalink)  
Old December 7th, 2011, 04:31 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Update: I've got it working by just changing a couple of lines.

In the onActivityResult method i inserted an earthquakes.clear and and aa.notify... so i refresh things with an empty screen before calling the refreshearthquakes (if you call this method with a magnitude greater than any present i think it generate an error and so fails to implement)

P.S. i needed to refresh on result cancelled also.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
earthquakes.clear();
aa.notifyDataSetChanged();

if (requestCode == SHOW_PREFERENCES)
//putting this clear in to clear the screen for a insight to what is happening
if (resultCode == Activity.RESULT_OK || resultCode == Activity.RESULT_CANCELED) {
updateFromPreferences();
refreshEarthquakes();
}
}

Last edited by docuk; December 7th, 2011 at 05:51 PM.. Reason: new info
  #27 (permalink)  
Old June 28th, 2012, 01:33 PM
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: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default PreferenceScreen

In case anybody else makes the dumb error I did here. I just spent about an hour looking over everything, and of course not focusing on the simplest thing. When I created the Preferences file in Eclipse, I chose the root element as "Preference" rather than the required "PreferenceScreen" This causes the same forced application close when selecting the preferences menu as did all the other omissions already mentioned in the other posts.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 8 Earthquake Notification woods620 BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 1 December 23rd, 2009 09:57 AM
Chapter 6 Earthquake using Provider abowman BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 0 March 9th, 2009 05:19 PM
Chapter 6 Sample Earthquake 2 sunrain BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 2 January 31st, 2009 04:06 PM





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