相关疑难解决方法(0)

如何声明Android偏好的类型?

我有一个preferences.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
 <EditTextPreference
  android:name="Sample"
  android:enabled="true"
  android:persistent="true"
  android:summary="Sample"
  android:defaultValue="3.0"
  android:title="Sample"
  android:key="sample" />
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

当我执行sp.getString("sample","3.0")时,它工作正常并返回一个字符串,但它不应该是一个字符串,它应该是一个浮点数.运行sp.getFloat("sample",3.0f)会抛出ClassCastException,因为它是一个字符串.

我应该在XML中放置什么以便将首选项存储为浮点数?

android preferences classcastexception

28
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

classcastexception ×1

preferences ×1