Using SwitchPreference's XML Attribute android:key causes my app to crash -
i have been trying use standard android switchpreference in preferencescreen of app. when define preferences.xml as:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" > <preference android:icon="@mipmap/ic_launcher" android:id="@+id/pref_taosettings_id" android:key="pref_taosettings_key" android:title="@string/pref_taosettings_title" /> <switchpreference android:defaultvalue="false" android:id="@+id/pref_playlevel_id" android:summary="level" android:summaryoff="@string/pref_playlevel_beginner" android:summaryon="@string/pref_playlevel_expert" android:title="@string/pref_playlevel_title" /> </preferencescreen>
when bring settings activity see following screen:
[ * removed sorry don't have reputation of 10, unable post screen shot. preferenceactivity appears title button @ top showing default icon. under that, in next listview position switchpreference toggle control. unable put text on control because minimum sdk version 7. removed * ]
if update preferences.xml read:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" > <preference android:icon="@mipmap/ic_launcher" android:id="@+id/pref_taosettings_id" android:key="pref_taosettings_key" android:title="@string/pref_taosettings_title" /> <switchpreference android:defaultvalue="false" android:id="@+id/pref_playlevel_id" android:key="pref_playlevel_key" android:summary="level" android:summaryoff="@string/pref_playlevel_beginner" android:summaryon="@string/pref_playlevel_expert" android:title="@string/pref_playlevel_title" /> </preferencescreen> note: using android:key
when recompile , run app, when click on menu item bring settings screen crash following logcat output (level debug):
07-21 14:51:34.798 25317-25317/com.pcphoneconnections.threeandout w/art﹕ before android 4.1, method int android.support.v7.internal.widget.listviewcompat.lookforselectableposition(int, boolean) have incorrectly overridden package-private method in android.widget.listview 07-21 14:51:36.957 25317-25317/com.pcphoneconnections.threeandout i/mainactivity::onoptionsitemselected﹕ id == r.id.action_settings 07-21 14:51:36.985 25317-25317/com.pcphoneconnections.threeandout w/inputeventreceiver﹕ attempted finish input event input event receiver has been disposed. 07-21 14:51:37.081 25317-25317/com.pcphoneconnections.threeandout i/settingsactivity﹕ oncreate(): entry. 07-21 14:51:37.106 25317-25317/com.pcphoneconnections.threeandout i/settingsactivity﹕ oncreatepreferencefromfragment(): entry. 07-21 14:51:37.111 25317-25317/com.pcphoneconnections.threeandout i/taopreferencefragment﹕ oncreate(): entry. 07-21 14:51:37.116 25317-25317/com.pcphoneconnections.threeandout d/androidruntime﹕ shutting down vm 07-21 14:51:37.118 25317-25317/com.pcphoneconnections.threeandout e/androidruntime﹕ fatal exception: main process: com.pcphoneconnections.threeandout, pid: 25317 java.lang.runtimeexception: unable start activity componentinfo{com.pcphoneconnections.threeandout/com.pcphoneconnections.threeandout.settingsactivity}: java.lang.classcastexception: java.lang.string cannot cast java.lang.boolean @ android.app.activitythread.performlaunchactivity(activitythread.java:2339) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2413) @ android.app.activitythread.access$800(activitythread.java:155) @ android.app.activitythread$h.handlemessage(activitythread.java:1317) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5356) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:905) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:700) caused by: java.lang.classcastexception: java.lang.string cannot cast java.lang.boolean @ android.app.sharedpreferencesimpl.getboolean(sharedpreferencesimpl.java:260) @ android.preference.preference.getpersistedboolean(preference.java:1691) @ android.preference.twostatepreference.onsetinitialvalue(twostatepreference.java:187) @ android.preference.preference.dispatchsetinitialvalue(preference.java:1376) @ android.preference.preference.onattachedtohierarchy(preference.java:1171) @ android.preference.preferencegroup.addpreference(preferencegroup.java:167) @ android.preference.preferencegroup.additemfrominflater(preferencegroup.java:108) @ android.preference.preferencegroup.additemfrominflater(preferencegroup.java:45) @ android.preference.genericinflater.rinflate(genericinflater.java:488) @ android.preference.genericinflater.inflate(genericinflater.java:326) @ android.preference.genericinflater.inflate(genericinflater.java:263) @ android.preference.preferencemanager.inflatefromresource(preferencemanager.java:272) @ android.preference.preferencefragment.addpreferencesfromresource(preferencefragment.java:299) @ com.pcphoneconnections.threeandout.settingsactivity$taopreferencefragment.oncreate(settingsactivity.java:265) @ android.app.fragment.performcreate(fragment.java:2031) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:863) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.app.backstackrecord.run(backstackrecord.java:834) @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1454) @ android.app.activity.performstart(activity.java:6025) @ android.app.activitythread.performlaunchactivity(activitythread.java:2302) at android.app.activitythread.handlelaunchactivity(activitythread.java:2413) at android.app.activitythread.access$800(activitythread.java:155) at android.app.activitythread$h.handlemessage(activitythread.java:1317) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5356) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:905) at com.android.internal.os.zygoteinit.main(zygoteinit.java:700) 07-21 14:51:39.324 25317-25317/com.pcphoneconnections.threeandout i/process﹕ sending signal. pid: 25317 sig: 9
i running android studio 1.2.2, following app settings.gradle configuration:
apply plugin: 'com.android.application'
android { compilesdkversion 22 buildtoolsversion "21.1.2" defaultconfig { applicationid "com.pcphoneconnections.threeandout" minsdkversion 7 targetsdkversion 22 versioncode 1 versionname "1.0" } //************************************************************************* // following appears identify location of pre-built shared // libraries, , disables automatic ndk-build call jni source. // instructions found on several different sites different ndk builds. //************************************************************************* sourcesets.main { jnilibs.srcdir 'src/main/libs' // use jni .so compiled manual ndk-build command jni.srcdirs = [] //disable automatic ndk-build call } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } } // repositories { // maven { // url "https://jitpack.io" // } // } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' }
finally, have following styles.xml configuration:
<resources> <!-- base application theme. --> <style name="apptheme" parent="theme.appcompat.light.darkactionbar"> <!-- customize theme here. --> <!-- <item name="android:buttonstyle">@style/button</item> --> </style> </resources>
if knows how solve crash problem, or can confirm issue, i'd know. can't find info on this, , right now, i'm thinking moving on checkbox type 2 state control. i'd rather use switch control though. looks better.
any appreciated.
thanks,
bob
after going on code make sure hadn't made stupid mistake, went original logcat output (shown above). going through output below java.lang.classcastexception: java.lang.string cannot cast java.lang.boolean, brought me 1 of android system library functions attempting make following cast:
boolean v = (boolean)mmap.get(key);
doing search on line of code brought me following post:
weird exception: cannot cast string boolean when using getboolean
following these instructions, uninstalled app device, , re-ran app, re-installed on device. once re-installed error went away. wow!!!
Comments
Post a Comment