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
  #11 (permalink)  
Old April 22nd, 2010, 09:04 PM
Registered User
 
Join Date: Apr 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Still Getting Exception after the Correction -for Chapter 6 Part 2 Earthquake project

When I am running the Chapter 6 part 2 (page 202) I get an exception when I click the "Preferences" button on the emulator. Here is the LogCat:




04-23 00:47:17.686: DEBUG/NetworkLocationProvider(58): onDataConnectionStateChanged 0
04-23 00:47:17.766: DEBUG/MobileDataStateTracker(58): default Received state= DISCONNECTED, old= CONNECTED, reason= radioTurnedOff, apnTypeList= default
04-23 00:47:17.818: DEBUG/NetworkStateTracker(58): setDetailed state, old =CONNECTED and new state=DISCONNECTED
04-23 00:47:18.787: DEBUG/ConnectivityService(58): ConnectivityChange for MOBILE: DISCONNECTED/DISCONNECTED
04-23 00:47:19.165: DEBUG/MobileDataStateTracker(58): default Received state= DISCONNECTED, old= DISCONNECTED, reason= gprsDetached, apnTypeList= default
04-23 00:47:19.546: DEBUG/NetworkLocationProvider(58): updateNetworkState(): Updating network state to 1
04-23 00:47:19.546: DEBUG/GpsLocationProvider(58): updateNetworkState unavailable info: NetworkInfo: type: MOBILE[UNKNOWN], state: DISCONNECTED/DISCONNECTED, reason: radioTurnedOff, extra: (none), roaming: false, failover: false, isAvailable: false
04-23 00:47:22.436: DEBUG/dalvikvm(261): Trying to load lib /system/lib/libjni_latinime.so 0x43d02bc8
04-23 00:47:22.476: DEBUG/dalvikvm(261): Added shared lib /system/lib/libjni_latinime.so 0x43d02bc8
04-23 00:47:24.827: DEBUG/dalvikvm(253): GC freed 3947 objects / 237720 bytes in 979ms
04-23 00:47:24.946: ERROR/ActivityThread(253): Failed to find provider info for android.server.checkin
04-23 00:47:24.995: WARN/Checkin(253): Can't update stat PHONE_GSM_REGISTERED: java.lang.IllegalArgumentException: Unknown URL content://android.server.checkin/stats
04-23 00:47:25.517: DEBUG/NetworkLocationProvider(58): onDataConnectionStateChanged 3
04-23 00:47:26.416: DEBUG/MobileDataStateTracker(58): default Received state= DISCONNECTED, old= DISCONNECTED, reason= (unspecified), apnTypeList= default
04-23 00:47:27.445: DEBUG/MccTable(253): updateMccMncConfiguration: mcc=310, mnc=260
04-23 00:47:27.467: DEBUG/MccTable(253): locale set to en_us
04-23 00:47:27.675: DEBUG/MccTable(253): WIFI_NUM_ALLOWED_CHANNESL set to 11
04-23 00:47:27.736: INFO/WifiService(58): WifiService trying to setNumAllowed to 11 with persist set to false
04-23 00:47:29.206: DEBUG/TelephonyProvider(253): Setting numeric '310260' to be the current operator
04-23 00:47:29.506: ERROR/ActivityThread(253): Failed to find provider info for android.server.checkin
04-23 00:47:29.536: WARN/Checkin(253): Can't update stat PHONE_GPRS_ATTEMPTED: java.lang.IllegalArgumentException: Unknown URL content://android.server.checkin/stats
04-23 00:47:29.605: DEBUG/NetworkLocationProvider(58): onDataConnectionStateChanged 3
04-23 00:47:29.786: DEBUG/MobileDataStateTracker(58): default Received state= CONNECTING, old= DISCONNECTED, reason= simLoaded, apnTypeList= *
04-23 00:47:29.866: DEBUG/NetworkStateTracker(58): setDetailed state, old =DISCONNECTED and new state=CONNECTING
04-23 00:47:29.976: DEBUG/ConnectivityService(58): ConnectivityChange for MOBILE: CONNECTING/CONNECTING
04-23 00:47:30.367: ERROR/ActivityThread(253): Failed to find provider info for android.server.checkin
04-23 00:47:30.377: WARN/Checkin(253): Can't update stat PHONE_GPRS_CONNECTED: java.lang.IllegalArgumentException: Unknown URL content://android.server.checkin/stats
04-23 00:47:30.446: DEBUG/NetworkLocationProvider(58): onDataConnectionStateChanged 3
04-23 00:47:30.715: DEBUG/MobileDataStateTracker(58): default Received state= CONNECTED, old= CONNECTING, reason= simLoaded, apnTypeList= *
04-23 00:47:30.797: DEBUG/MobileDataStateTracker(58): CONNECTED event did not supply interface name.
04-23 00:47:30.817: DEBUG/NetworkStateTracker(58): setDetailed state, old =CONNECTING and new state=CONNECTED
04-23 00:47:30.845: DEBUG/ConnectivityService(58): ConnectivityChange for MOBILE: CONNECTED/CONNECTED
04-23 00:47:30.906: VERBOSE/NetworkStateTracker(58): Setting TCP values: [4094,87380,110208,4096,16384,110208] which comes from [net.tcp.buffersize.umts]
04-23 00:47:30.967: DEBUG/MobileDataStateTracker(58): replacing old mInterfaceName (null) with null for mms
04-23 00:47:31.166: DEBUG/MobileDataStateTracker(58): replacing old mInterfaceName (null) with null for supl
04-23 00:47:31.206: DEBUG/MobileDataStateTracker(58): replacing old mInterfaceName (null) with null for dun
04-23 00:47:31.266: DEBUG/MobileDataStateTracker(58): replacing old mInterfaceName (null) with null for hipri
04-23 00:47:31.547: DEBUG/dalvikvm(240): GC freed 4507 objects / 219992 bytes in 2342ms
04-23 00:47:31.646: DEBUG/ConnectivityService(58): adding dns 10.0.2.3 for MOBILE
04-23 00:47:31.856: DEBUG/NetworkLocationProvider(58): updateNetworkState(): Updating network state to 2
04-23 00:47:31.876: DEBUG/GpsLocationProvider(58): updateNetworkState available info: NetworkInfo: type: MOBILE[UMTS], state: CONNECTED/CONNECTED, reason: simLoaded, extra: internet, roaming: false, failover: false, isAvailable: true
04-23 00:47:31.916: DEBUG/GpsLocationProvider(58): NetworkThread wait for 9223372036854775807ms
04-23 00:47:35.345: DEBUG/dalvikvm(240): GC freed 10939 objects / 493296 bytes in 362ms
04-23 00:47:38.895: DEBUG/dalvikvm(240): GC freed 17360 objects / 794568 bytes in 435ms
04-23 00:47:39.797: INFO/ARMAssembler(58): generated scanline__00000177:03515104_00001A01_00000000 [ 73 ipp] (98 ins) at [0x368c18:0x368da0] in 2367899 ns
04-23 00:47:39.867: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$ Proxy@43cb4800
04-23 00:47:40.377: DEBUG/dalvikvm(106): GC freed 575 objects / 30440 bytes in 2313ms
04-23 00:47:43.076: DEBUG/dalvikvm(163): GC freed 1729 objects / 82424 bytes in 311ms
04-23 00:47:46.285: WARN/KeyCharacterMap(240): No keyboard for id 0
04-23 00:47:46.305: WARN/KeyCharacterMap(240): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-23 00:47:48.196: INFO/ActivityManager(58): Starting activity: Intent { cmp=com.paad.earthquake/.Preferences }
04-23 00:47:48.675: DEBUG/AndroidRuntime(240): Shutting down VM
04-23 00:47:48.685: WARN/dalvikvm(240): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-23 00:47:48.731: ERROR/AndroidRuntime(240): Uncaught handler: thread main exiting due to uncaught exception
04-23 00:47:48.896: ERROR/AndroidRuntime(240): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.earthquake/com.paad.earthquake.Preferences}: android.view.InflateException: Binary XML file line #2: Error inflating class PreferencesScreen
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2496)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2512)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.app.ActivityThread.access$2200(ActivityThr ead.java:119)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1863)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.os.Handler.dispatchMessage(Handler.java:99 )
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.os.Looper.loop(Looper.java:123)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.app.ActivityThread.main(ActivityThread.jav a:4363)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at java.lang.reflect.Method.invoke(Method.java:521)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:860)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:618)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at dalvik.system.NativeStart.main(Native Method)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class PreferencesScreen
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.GenericInflater.createItemFromT ag(GenericInflater.java:441)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.GenericInflater.inflate(Generic Inflater.java:317)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.GenericInflater.inflate(Generic Inflater.java:263)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.PreferenceManager.inflateFromRe source(PreferenceManager.java:254)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.PreferenceActivity.addPreferenc esFromResource(PreferenceActivity.java:253)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at com.paad.earthquake.Preferences.onCreate(Preferenc es.java:18)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1047)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2459)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): ... 11 more
04-23 00:47:48.896: ERROR/AndroidRuntime(240): Caused by: java.lang.ClassNotFoundException: android.preference.PreferencesScreen in loader dalvik.system.PathClassLoader@43d02888
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at dalvik.system.PathClassLoader.findClass(PathClassL oader.java:243)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at java.lang.ClassLoader.loadClass(ClassLoader.java:5 73)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at java.lang.ClassLoader.loadClass(ClassLoader.java:5 32)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.GenericInflater.createItem(Gene ricInflater.java:375)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.GenericInflater.onCreateItem(Ge nericInflater.java:417)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): at android.preference.GenericInflater.createItemFromT ag(GenericInflater.java:428)
04-23 00:47:48.896: ERROR/AndroidRuntime(240): ... 18 more
04-23 00:47:49.137: INFO/Process(58): Sending signal. PID: 240 SIG: 3
04-23 00:47:49.156: INFO/dalvikvm(240): threadid=7: reacting to signal 3
04-23 00:47:49.317: INFO/dalvikvm(240): Wrote stack trace to '/data/anr/traces.txt'
04-23 00:47:52.317: INFO/Process(240): Sending signal. PID: 240 SIG: 9
04-23 00:47:52.607: ERROR/gralloc(58): [unregister] handle 0x3867d0 still locked (state=40000001)
04-23 00:47:52.787: INFO/ActivityManager(58): Process com.paad.earthquake (pid 240) has died.
04-23 00:47:52.796: INFO/WindowManager(58): WIN DEATH: Window{43ddf000 AtchDlg:com.paad.earthquake/com.paad.earthquake.Earthquake paused=false}
04-23 00:47:52.916: INFO/WindowManager(58): WIN DEATH: Window{43d03c58 com.paad.earthquake/com.paad.earthquake.Earthquake paused=false}
04-23 00:47:53.197: INFO/ActivityManager(58): Start proc com.paad.earthquake for activity com.paad.earthquake/.Earthquake: pid=279 uid=10026 gids={3003, 1015}
04-23 00:47:54.147: DEBUG/ddm-heap(279): Got feature list request
04-23 00:47:54.545: INFO/UsageStats(58): Unexpected resume of com.paad.earthquake while already resumed in com.paad.earthquake
04-23 00:47:54.745: INFO/ActivityManager(58): Displayed activity com.paad.earthquake/.Earthquake: 1740 ms (total 6371 ms)
04-23 00:47:55.896: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 240 uid 10026



Please help me trouble shoot this. As I have tried the corrections as given to me by previous response to this post.

Christian
  #12 (permalink)  
Old April 26th, 2010, 06:11 AM
Reto's Avatar
Wrox Author
Points: 287, Level: 5
Points: 287, Level: 5 Points: 287, Level: 5 Points: 287, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2008
Location: London, United Kingdom
Posts: 61
Thanks: 1
Thanked 7 Times in 6 Posts
Default

It looks like a spelling error in your XML preference definition. It should be PreferenceScreen rather than PreferencesScreen. Hope that helps!
__________________
Reto Meier

Author, "Professional Android 4 Application Development", Wrox, ©2012, 2010, 2008
@retomeier
  #13 (permalink)  
Old April 27th, 2010, 09:39 PM
Registered User
 
Join Date: Apr 2010
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your reply. Yes I did fix this. But I am still having an exception. Here is the LogCat:

04-28 01:28:26.348: WARN/ResourceType(54): Resources don't contain package for resource number 0x7f060001
04-28 01:28:28.220: DEBUG/AndroidRuntime(218): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-28 01:28:28.229: DEBUG/AndroidRuntime(218): CheckJNI is ON
04-28 01:28:28.688: DEBUG/AndroidRuntime(218): --- registering native functions ---
04-28 01:28:30.128: INFO/ActivityManager(54): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.paad.earthquake/.Earthquake }
04-28 01:28:30.250: DEBUG/AndroidRuntime(218): Shutting down VM
04-28 01:28:30.250: DEBUG/dalvikvm(218): DestroyJavaVM waiting for non-daemon threads to exit
04-28 01:28:30.299: DEBUG/dalvikvm(218): DestroyJavaVM shutting VM down
04-28 01:28:30.299: DEBUG/dalvikvm(218): HeapWorker thread shutting down
04-28 01:28:30.308: DEBUG/dalvikvm(218): HeapWorker thread has shut down
04-28 01:28:30.308: DEBUG/jdwp(218): JDWP shutting down net...
04-28 01:28:30.308: INFO/jdwp(218): adbd disconnected
04-28 01:28:30.380: ERROR/AndroidRuntime(218): ERROR: thread attach failed
04-28 01:28:30.413: DEBUG/dalvikvm(218): VM cleaning up
04-28 01:28:30.629: DEBUG/dalvikvm(218): LinearAlloc 0x0 used 636388 of 5242880 (12%)
04-28 01:28:30.978: INFO/ActivityManager(54): Start proc com.paad.earthquake for activity com.paad.earthquake/.Earthquake: pid=225 uid=10024 gids={3003, 1015}
04-28 01:28:31.790: DEBUG/AndroidRuntime(225): Shutting down VM
04-28 01:28:31.790: WARN/dalvikvm(225): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-28 01:28:31.820: ERROR/AndroidRuntime(225): Uncaught handler: thread main exiting due to uncaught exception
04-28 01:28:31.869: ERROR/AndroidRuntime(225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.earthquake/com.paad.earthquake.Earthquake}: java.lang.ClassCastException: java.lang.Integer
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2496)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2512)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.ActivityThread.access$2200(ActivityThr ead.java:119)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1863)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.os.Handler.dispatchMessage(Handler.java:99 )
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.os.Looper.loop(Looper.java:123)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.ActivityThread.main(ActivityThread.jav a:4363)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at java.lang.reflect.Method.invoke(Method.java:521)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:860)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:618)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at dalvik.system.NativeStart.main(Native Method)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): Caused by: java.lang.ClassCastException: java.lang.Integer
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.ApplicationContext$SharedPreferencesIm pl.getString(ApplicationContext.java:2572)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at com.paad.earthquake.Earthquake.updateFromPreferenc es(Earthquake.java:187)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at com.paad.earthquake.Earthquake.onCreate(Earthquake .java:76)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1047)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2459)
04-28 01:28:31.869: ERROR/AndroidRuntime(225): ... 11 more
04-28 01:28:31.928: INFO/Process(54): Sending signal. PID: 225 SIG: 3
04-28 01:28:31.938: INFO/dalvikvm(225): threadid=7: reacting to signal 3
04-28 01:28:32.018: INFO/dalvikvm(225): Wrote stack trace to '/data/anr/traces.txt'
04-28 01:28:32.389: DEBUG/ddm-heap(119): Got feature list request
04-28 01:28:32.479: DEBUG/ddm-heap(136): Got feature list request
04-28 01:28:32.579: DEBUG/ddm-heap(154): Got feature list request
04-28 01:28:32.719: DEBUG/ddm-heap(166): Got feature list request
04-28 01:28:32.848: DEBUG/ddm-heap(185): Got feature list request


Please help.

Thanks.

Christian
  #14 (permalink)  
Old April 29th, 2010, 10:39 AM
Reto's Avatar
Wrox Author
Points: 287, Level: 5
Points: 287, Level: 5 Points: 287, Level: 5 Points: 287, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2008
Location: London, United Kingdom
Posts: 61
Thanks: 1
Thanked 7 Times in 6 Posts
Default

It's complaining about a type mismatch within the updateFromPreferences method. None of the values being pulled from the SharedPreferences are strings -- PREF_MIN_MAG and PREF_UPDATE_FREQ are both ints, and PREF_AUTO_UPDATE is a boolean.

Check to make sure that the first two calls are to prefs.getInt not prefs.getString
__________________
Reto Meier

Author, "Professional Android 4 Application Development", Wrox, ©2012, 2010, 2008
@retomeier
  #15 (permalink)  
Old May 11th, 2010, 03:00 PM
Registered User
 
Join Date: May 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Reto,

I struggled with this problem too, and the problem is the exact opposite of what you're saying here.

The Book left us at page 202 with this in our updateFromPreferences() function:
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;
    	}
This fails because the preferences, even the integer ones, are stored as strings in userpreferences.xml. In your downloaded code, you fix this by providing this code:

Code:
int minMagIndex = Integer.parseInt(prefs.getString(Preferences.PREF_MIN_MAG, "0"));
    	if(minMagIndex < 0) {
    		minMagIndex =0;
    	}
    	
    	int freqIndex = Integer.parseInt(prefs.getString(Preferences.PREF_UPDATE_FREQ,"0"));
    	if(freqIndex < 0)  {
    		freqIndex = 0;
    	}
Note the pull-as-string-and-parse-as-int difference than where the book leaves us.

Thanks for the great book, this is just a minor problem I found in it. One suggestion I have as well is when you provide code samples that change, in some places you highlight the changed lines, in other places you do not. Being consistent and highlighting the changed lines would be great.

Thanks again for the great book!
Mike
  #16 (permalink)  
Old May 11th, 2010, 03:28 PM
Registered User
 
Join Date: May 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just to add, and I dunno if my last post, left alone, with this change would fix the problem, but the book also left us with this in the arrays.xml file:

Code:
<array name="magnitude">
    <item>3</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
  </array>
 <array name="update_freq_values">
    <item>1</item>
    <item>5</item>
    <item>10</item>
    <item>15</item>
    <item>60</item>
  </array>
Whereas the example code changes those two to "string-array"s.

After applying this fix (changing the arrays to string-arrays), my preferences screen that we're supposed to have at page 202 finally works. (applied both this fix and the fix in my post above).

Thanks again,
Mike
  #17 (permalink)  
Old June 15th, 2010, 03:30 AM
Authorized User
Points: 123, Level: 2
Points: 123, Level: 2 Points: 123, Level: 2 Points: 123, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2010
Posts: 18
Thanks: 0
Thanked 6 Times in 3 Posts
Default Solution Recap?

Let me try to recap what I think the solution to this is.
  1. There are some required code changes that were not mentioned in the book.
    • The all of the arrays defined in arrays.xml must be string-arrays. The PreferenceActivity works only with Strings -- you can't use int (even though the data is ints).
    • Code dealing with the reading and writing of the preferences must be updated to read and write strings and not ints.
    • onActivityResult() should be updated to not check for Activity.RESULT_OK. I'm guessing it's always Activity.RESULT_CANCELED when hitting the back button.
  2. I believe that if you ran the program using the first version, you may have already written integer preferences (and not strings) into the persistent storage for the application. Reinstalling the application doesn't seem to wipe the previously saved persistent data, so when you try to launch the preferences activity, it crashes with the ClassCastException -- it's finding integer data that was previously saved and trying to cast it to String. If you have this problem, try selecting the 'wipe user data' option in the emulator. That seems to blow away the previously saved data. The next time the program runs, when it tries to read the persisted data, it's not there and the method returns the default value (instead of throwing an exception). I suppose another way to resolve this is to change the property names of the preferences, but then you'd still have the old data under the old property name lingering around.
Let me say that I'm new to this too; this recap is my personal understanding. I welcome any corrections.

Last edited by DrGaribaldi; June 15th, 2010 at 02:53 PM..
The Following User Says Thank You to DrGaribaldi For This Useful Post:
  #18 (permalink)  
Old July 14th, 2010, 03:30 PM
Registered User
 
Join Date: Oct 2009
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default

I am having the same issue. Here is the logcat error that is logged when the Preferences menu item is clicked:

Code:
07-14 15:22:44.977: ERROR/AndroidRuntime(30711): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.iamcorbin.earthquake/net.iamcorbin.earthquake.UserPreferences}: java.lang.ClassCastException: java.lang.Integer
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.os.Looper.loop(Looper.java:123)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.ActivityThread.main(ActivityThread.java:4595)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at java.lang.reflect.Method.invoke(Method.java:521)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at dalvik.system.NativeStart.main(Native Method)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711): Caused by: java.lang.ClassCastException: java.lang.Integer
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.ApplicationContext$SharedPreferencesImpl.getString(ApplicationContext.java:2622)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.Preference.getPersistedString(Preference.java:1250)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.ListPreference.onSetInitialValue(ListPreference.java:232)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.Preference.dispatchSetInitialValue(Preference.java:1173)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.Preference.onAttachedToHierarchy(Preference.java:985)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.GenericInflater.inflate(GenericInflater.java:326)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.GenericInflater.inflate(GenericInflater.java:263)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:254)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:259)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at net.iamcorbin.earthquake.UserPreferences.onCreate(UserPreferences.java:10)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-14 15:22:44.977: ERROR/AndroidRuntime(30711):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
However, if I modify userpreferences.xml to only include the CheckBoxPreference and not the ListPreferences it loads fine. Here is my ListPreference code that seems to be triggering the error:

Code:
<ListPreference android:key="PREF_UPDATE_FREQ"
  	android:title="@string/update_freq_prompt" android:summary="@string/update_freq_desc"
  	android:entries="@array/update_freq_options" android:entryValues="@array/update_freq_values"
  	android:dialogTitle="@string/update_freq_prompt"
  	android:defaultValue="60"
  />
  <ListPreference android:key="PREF_MIN_MAG"
  	android:title="@string/min_quake_mag_prompt" android:summary="@string/min_quake_mag_desc"
  	android:entries="@array/magnitude_options" android:entryValues="@array/magnitude_values"
  	android:dialogTitle="@string/min_quake_mag_prompt"
  	android:defaultValue="3"
  />
and here is my 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>
	<array name="update_freq_values">
		<item>1</item>
		<item>5</item>
		<item>10</item>
		<item>15</item>
		<item>60</item>
	</array>
	
	<string-array name="magnitude_options">
		<item>3</item>
		<item>4</item>
		<item>5</item>
		<item>6</item>
		<item>7</item>
		<item>8</item>
	</string-array>
	<array name="magnitude_values">
		<item>3</item>
		<item>4</item>
		<item>5</item>
		<item>6</item>
		<item>7</item>
		<item>8</item>
	</array>
</resources>
and strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Earthquake</string>
    <string name="quake_feed">http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml</string>
    <string name="menu_update">Refresh Earthquakes</string>
    
    <string name="menu_preferences">Preferences</string>
    <string name="auto_update_prompt">Auto Update?</string>
    <string name="auto_update_desc">Select to turn on automatic updates</string>
    <string name="update_freq_prompt">Update Frequency</string>
    <string name="update_freq_desc">Frequency at which to refresh earthquake list</string>
    <string name="min_quake_mag_prompt">Minimum Quake Magnitude</string>
    <string name="min_quake_mag_desc">Select the minimum magnitude earthquake to report</string>
    <string name="ok">OK</string>
    <string name="cancel">CANCEL</string>
</resources>
EDIT: Sorry, I didn't see the 2nd page before I posted my response. I just changed the arrays to string-arrays and used Integer.parseInt(); I was getting the ClassCastException. I am developing on a HTC EVO so I uninstalled the application through the phone menu and then reinstalled through Eclipse and now it works fine. Thanks!

Last edited by IAmCorbin; July 14th, 2010 at 08:00 PM..
  #19 (permalink)  
Old July 14th, 2010, 08:14 PM
Registered User
 
Join Date: Oct 2009
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default

I spoke too soon. Having a new problem now. After uninstalling and reinstalling the app runs just fine and I can open the preferences menu option and select the different values in the preferences or update the earthquake list. If I exit the app and then run it again I get this:
Code:
07-14 20:12:14.389: ERROR/AndroidRuntime(2413): Caused by: java.lang.ArrayIndexOutOfBoundsException
which is being triggered by this line
Code:
this.minimumMagnitude = Integer.parseInt(minMagValues[minMagIndex]);
If I uninstall the app again and reinstall it works fine, but then triggers the same error on any subsequent running.

EDIT:
I was still pulling the strings from the arrays.xml as well as the preferences and this was my problem I dropped the extra code and assign the class variables directly

Last edited by IAmCorbin; July 15th, 2010 at 10:48 PM.. Reason: Solved own issue
  #20 (permalink)  
Old August 26th, 2010, 10:15 AM
Registered User
 
Join Date: Aug 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I have the a problem from the same sample ("The application com.paad.earthquake (process com.paad.earthquake) has stopped unexpectedly. Please try again." ) with the latest sample codes. And this problem occurs every Earthquake sample after the Chapter 6 Sample Earthquake. Here is the log;

Code:
08-26 14:00:16.975: ERROR/AndroidRuntime(985): FATAL EXCEPTION: main
08-26 14:00:16.975: ERROR/AndroidRuntime(985): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.earthquake/com.paad.earthquake.Earthquake}: java.lang.ClassCastException: java.lang.Integer
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.os.Looper.loop(Looper.java:123)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at java.lang.reflect.Method.invokeNative(Native Method)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at java.lang.reflect.Method.invoke(Method.java:521)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at dalvik.system.NativeStart.main(Native Method)
08-26 14:00:16.975: ERROR/AndroidRuntime(985): Caused by: java.lang.ClassCastException: java.lang.Integer
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.ContextImpl$SharedPreferencesImpl.getString(ContextImpl.java:2699)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at com.paad.earthquake.Earthquake.updateFromPreferences(Earthquake.java:236)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at com.paad.earthquake.Earthquake.onCreate(Earthquake.java:75)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-26 14:00:16.975: ERROR/AndroidRuntime(985):     ... 11 more




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.