View Single Post
  #11 (permalink)  
Old August 18th, 2010, 01:34 PM
juniper juniper is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Thanks: 1
Thanked 2 Times in 2 Posts
Default Changes to get Chapter 10 Earthquake 3 Working

Hi,
I started seeing the same exception, java.lang.ClassCastException: java.lang.String.

By creating a new project, mellowfellow, you cleared out the existing preferences data. The preference data is stored as strings, but if you look at the onStartCommand in EarthquakeService, it's attempting to read an int.

Run Reto's sample code, Chapter 10 Earthquake 3, and change the preferences using the preferences menu. Restart the app and you will see the ClassCastException again.

To fix the code replace this:
Code:
    int minMagIndex = prefs.getInt(Preferences.PREF_MIN_MAG, 0);
    if (minMagIndex < 0)
      minMagIndex = 0;

    int freqIndex = prefs.getInt(Preferences.PREF_UPDATE_FREQ, 0);
    if (freqIndex < 0)
      freqIndex = 0;

    boolean autoUpdate = 
      prefs.getBoolean(Preferences.PREF_AUTO_UPDATE, false);

    Resources r = getResources();
    int[] freqValues = r.getIntArray(R.array.update_freq_values);

    int updateFreq = freqValues[freqIndex];
with this:
Code:
    int updateFreq = Integer.parseInt(prefs.getString(Preferences.PREF_UPDATE_FREQ, "60"));

    boolean autoUpdate = 
      prefs.getBoolean(Preferences.PREF_AUTO_UPDATE, false);
Reply With Quote