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
| 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 December 23rd, 2010, 07:03 AM
Registered User
 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 6, Earthquake 2 [preferences not working]

Somehow I get an Error and I can't get the clue where it comes from.
First I changed the name of the 'Preferences' class into 'UserPreferences' and got an error, but that was most likely because I forgot to declare the new 'UserPreferences' in the manifest.xml.

But now when I hit Menu --> Preferences this error appears.
Seems that it is coming somehow from the resource file during the inflate procedure.

Here is the LogCat:
12-23 10:57:12.486: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.paad.earthquake/.UserPreferences }
12-23 10:57:12.606: WARN/Resources(466): Converting to string: TypedValue{t=0x10/d=0x3c a=-1}
12-23 10:57:12.626: DEBUG/AndroidRuntime(466): Shutting down VM
12-23 10:57:12.626: WARN/dalvikvm(466): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): FATAL EXCEPTION: main
12-23 10:57:12.646: ERROR/AndroidRuntime(466): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.earthquake/com.paad.earthquake.UserPreferences}: java.lang.ClassCastException: java.lang.Integer
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2663)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2679)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.ActivityThread.access$2300(ActivityThr ead.java:125)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:2033)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.os.Handler.dispatchMessage(Handler.java:99 )
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.os.Looper.loop(Looper.java:123)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at java.lang.reflect.Method.invoke(Method.java:521)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:868)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at dalvik.system.NativeStart.main(Native Method)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): Caused by: java.lang.ClassCastException: java.lang.Integer
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.ContextImpl$SharedPreferencesImpl.getS tring(ContextImpl.java:2699)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.Preference.getPersistedString(P reference.java:1249)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.ListPreference.onSetInitialValu e(ListPreference.java:232)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.Preference.dispatchSetInitialVa lue(Preference.java:1172)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.Preference.onAttachedToHierarch y(Preference.java:984)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.PreferenceGroup.addPreference(P referenceGroup.java:156)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.PreferenceGroup.addItemFromInfl ater(PreferenceGroup.java:97)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.PreferenceGroup.addItemFromInfl ater(PreferenceGroup.java:38)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.GenericInflater.rInflate(Generi cInflater.java:488)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.GenericInflater.inflate(Generic Inflater.java:326)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.GenericInflater.inflate(Generic Inflater.java:263)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.PreferenceManager.inflateFromRe source(PreferenceManager.java:251)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.preference.PreferenceActivity.addPreferenc esFromResource(PreferenceActivity.java:262)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at com.paad.earthquake.UserPreferences.onCreate(UserP references.java:19)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1047)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2627)
12-23 10:57:12.646: ERROR/AndroidRuntime(466): ... 11 more
12-23 10:57:12.666: WARN/ActivityManager(59): Force finishing activity com.paad.earthquake/.UserPreferences
12-23 10:57:12.676: WARN/ActivityManager(59): Force finishing activity com.paad.earthquake/.Earthquake

thanks in advance for any help.
btw. merry christmas!!
Reply With Quote
  #2 (permalink)  
Old December 28th, 2010, 10:21 PM
Registered User
 
Join Date: Dec 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default I hava a same error with you

I don't kown how to do.who can help us
Reply With Quote
  #3 (permalink)  
Old January 5th, 2011, 01:06 PM
Registered User
 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Didn't have time to work on the problem.
Will give it another try tonight. Maybe tomorrow I can provide a solution of the problem
Reply With Quote
  #4 (permalink)  
Old January 6th, 2011, 08:26 AM
Registered User
 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get more and more confused, maybe someone can give me a hint.

First, ...
the author is using sometimes "preferences" sometimes "userpreferences", but that's not the problem.

Second, ...
also in the code snippets in the 'arrays.xml', the 'magnitude' (same with 'update_freq_values') is a
Code:
<string-array name="magnitude>
in the book it's a
Code:
<array name="magnitude>
Third, ...
in the Earthquake class my 'updateFromPreferences' method looks like this:
Code:
    private void updateFromPreferences() {
    	Context context = getApplicationContext();
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    	
    	int minMagIndex = prefs.getInt(UserPreferences.PREF_MIN_MAG, 0);
    	if(minMagIndex < 0)
    		minMagIndex = 0;
    	
    	int freqIndex = prefs.getInt(UserPreferences.PREF_UPDATE_FREQ, 0);
    	if(freqIndex < 0)
    		freqIndex = 0;
    	
    	autoUpdate = prefs.getBoolean(UserPreferences.PREF_AUTO_UPDATE, false);
    	
    	Resources r = getResources();
    	int[] minMagValues = r.getIntArray(R.array.magnitude);
    	int[] freqValues = r.getIntArray(R.array.update_freq_values);
    	
    	minimumMagnitude = minMagValues[minMagIndex];
    	updateFreq = freqValues[freqIndex];
    }
In the authors code snippets it is like this:
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);
    }
If I am correct, it is not mentioned in the book that we should change the method 'updateFromPreferences'. Whenever I change it to the authors code taken from the code snippets, the application cannot be started because the method is called in 'onCreate", forced close and so on.

Can someone help me, how should the 'updateFromPreferences' method look like?
Reply With Quote
  #5 (permalink)  
Old January 9th, 2011, 12:40 AM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Solution

I've just come across and dealt with the same problem myself. After a bit of research I found out that there are a number of problems with the code that need to be corrected. Essentially the code given in the book is incomplete (I haven't downloaded the code from the website, so that might more correct).
  1. The main problem is that if you use an XML definition for the preferences along with PreferenceActivity, all the arrays have to be string arrays. In the original Earthquake exercise, the magnitude and update_freq_values arrays are integer arrays.

    See this StackOverflow question and this Android bug for more details.

    You have to change these two arrays to string arrays, and then in Earthquake.updateFromPreferences you have to convert to integers

    Code:
    minmagnitude = Integer.parseInt(prefs.getString(UserPreferences.PREF_MIN_MAG, "0"));
    updatefreq = Integer.parseInt(prefs.getString(UserPreferences.PREF_UPDATE_FREQ, "0"));
    Warning: be aware that if you have already run the original exercise on the same hardware / emulator instance, then SharedPreferences will already have an integer value stored for the magnitude and update frequency options. So, when you first run your modified PreferenceActivity it will fail with an exception when it tries to read the previously stored values, because it is expecting a string and getting an integer. To get around this, you can clear the existing SharedPreference values first, by running the following code (in OnCreate for example (you only need to do this once, and then can delete this code):

    Code:
    Editor edit = prefs.edit();
    edit.clear();
    edit.commit();
  2. In the original exercise, the index into the arrays is stored in SharedPrefs, and then the index is retrieved in updateFromPreferences and used to look up the actual value in the values array.

    When you move to string-arrays, the PreferenceActivity doesn't store the index into the string-array but rather the actual value, in SharedPrefs. So you no longer need to do the following lookup:

    Code:
    int[] minMagValues = r.getIntArray(R.array.magnitude);
    minimumMagnitude = minMagValues[minMagIndex]
    instead you can now just use the value directly:

    Code:
    minmagnitude = Integer.parseInt(prefs.getString(UserPreferences.PREF_MIN_MAG, "0"));
  3. With the code as it is in the book, the returned result from the PreferenceActivity is never used, so your code will never refresh the earthquakes list after you return from the preferences screen. The reason is that there is no OK button any longer, and the standard way of returning from the preferences screen (using the back button) will give a result of Activity.RESULT_CANCEL in the onActivityResult handler. You should not use startActivityWithResult with a PreferenceActivity, but instead should use startActivity and then handle the Shared Preference Change Listener (as detailed in the book on page 201).

    Code:
    public class Earthquake 
    	extends Activity 
    	implements OnSharedPreferenceChangeListener {
    
    [...]
    
    	public void onCreate(Bundle savedInstanceState) {
    		[...]
    		SharedPreferences prefs = 
    			PreferenceManager.getDefaultSharedPreferences(
    				getApplicationContext());
    		prefs.registerOnSharedPreferenceChangeListener(this);
    		[...]
    	}
    
    	public boolean onOptionsItemSelected(MenuItem item) {
    		[...]
    		switch (item.getItemId()) {
    			[...]
    			case (MENU_PREFERENCES): {
    				Intent i = new Intent(this, UserPreferences.class);
    				startActivity(i);
    				return true;
    			}
    		}
    		[...]
    	}
    
    	@Override
    	public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    		updateFromPreferences();
    		refreshEarthquakes();
    	}

Hope that helps. Feel free to PM me and I can send you my code, which works correctly.
Reply With Quote
  #6 (permalink)  
Old January 9th, 2011, 12:45 AM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also see this thead: Problem Running Chapter 6 Earthquake
Reply With Quote
  #7 (permalink)  
Old January 9th, 2011, 11:35 AM
Registered User
 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

great, tried out your changes and it is working.

somehow i am slow with getting the big picture itself.
i read the book but cannot understand what i am actually doing when i hack in the code. maybe i should change the way of studying :(

again, thanks very much for helping my out here. works fine now!


Reply With Quote
  #8 (permalink)  
Old January 9th, 2011, 09:09 PM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No problem, I'm glad it helped! Don't worry, give it more time and practice, and it will all start to come together...
Reply With Quote
  #9 (permalink)  
Old January 7th, 2013, 10:59 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: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello deebo,

I tried your changes on chapter 9, which works perfect, apart from the preferences where the app stops.
Could you please post your code, it would actually save my life :)

Thank you
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
Chapter11 Earthquake project did not build. How I got it working burtbick BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 2 October 30th, 2010 05:21 AM
Chapter 9 Earthquake 1 MellowFellow BOOK: Professional Android 2 Application Development 14 August 23rd, 2010 04:40 PM
chapter 5 Earthquake EricTapia BOOK: Professional Android 2 Application Development 2 April 14th, 2010 12:47 AM
Chap6 - Earthquake not updating when nothing matches preferences cartoon BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 1 January 16th, 2010 05:41 AM



All times are GMT -4. The time now is 11:13 AM.


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