共享首选项返回String而不是Boolean

时间:2016-09-16 17:14:13

标签: android android-emulator sharedpreferences

我的应用程序应该读取此默认共享首选项文件中的布尔值:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <boolean android:key="Market"
        android:defaultValue= "true" />
    <boolean android:key="Farmacie"
        android:defaultValue="true"/>
    <boolean android:key="Musei"
        android:defaultValue="true"/>
    <boolean android:key="Ristoranti"
        android:defaultValue="true"/>
    <boolean android:key="Banche"
        android:defaultValue="true"/>
    <boolean android:key="Bar"
        android:defaultValue="true"/>
    <boolean android:key="Avvio"
        android:defaultValue="true"/>
    <boolean android:key="Background"
        android:defaultValue="true"/>
</PreferenceScreen>

这是代码:

SharedPreferences pref = context.getSharedPreferences("myPref", Context.MODE_PRIVATE);
    if(pref.getBoolean("Avvio", true))
    {
        Intent startServiceIntent = new Intent(context, LocationService.class);
        context.startService(startServiceIntent);
    }

当我在手机上运行时,一切似乎都运行良好但是当我在AVD(Nexus 5,6.0)上测试时,我得到了这个例外:

FATAL EXCEPTION: main
                                                         Process: so2.poifinder, PID: 1861
                                                         java.lang.RuntimeException: Unable to start receiver so2.poifinder.RunAtStartup: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean

我还尝试将XML文件中的值保存为CheckBoxSharedPreference但没有任何更改。我想知道为什么模拟器在解析文件时返回一个String值,而我的智能手机返回一个布尔值。我应该将所有内容存储为字符串,以确保不会引发异常吗?

修改 这是非常愚蠢的事情:由于某种原因,在模拟器上还安装了旧版本的应用程序(具有相同的名称),这是提出异常的一个(因为我将值存储为XML文件中的EditText)。我只需要存储CheckBoxPreference而不是布尔值

0 个答案:

没有答案
相关问题