获取 ClassCastException: java.lang.Integer 无法在不进行转换的情况下转换为 java.lang.String 错误

THE*_*E-E 6 java android casting exception sharedpreferences

我正在开发一个 Android 应用程序,但无法找出我所犯的错误或错误。我找了4个多小时的错误。

我在使用 SharedPreferences 时遇到以下异常:

E/AndroidRuntime: FATAL EXCEPTION: main
   Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
     at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:235)
     at de.immozukunft.quicksteuer.QuickSteuerActivity.updateTextView(QuickSteuerActivity.java:56)
     at de.immozukunft.quicksteuer.QuickSteuerActivity.onCreate(QuickSteuerActivity.java:36)
Run Code Online (Sandbox Code Playgroud)

但根据我的理解,我并没有在这方面进行演员阵容。第 36 行是updateTextView()方法的调用。

private void updateTextView() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    int legalForm = Integer.parseInt(prefs.getString("legal_form", "1"));
    boolean industrialTax = prefs.getBoolean("industrial_tax", false);
    boolean turnoverTax = prefs.getBoolean("turnover_tax", false);
    boolean disbursal = prefs.getBoolean("disbursal", false);

    String tmp = prefs.getString("capitalownership", "0"); //this is line 56

    int capitalOwnership = Integer.parseInt(tmp);
    int estIncome = Integer.parseInt(prefs.getString("est_income", "0"));
}
Run Code Online (Sandbox Code Playgroud)

我在 XML 文件中的偏好如下:

<EditTextPreference
android:defaultValue="0"
android:dialogTitle="@string/capitalownership"
android:inputType="number"
android:key="capitalownership"
android:title="@string/capitalownership" />
Run Code Online (Sandbox Code Playgroud)

我需要发布完整的代码和完整的错误列表。

我对 Android 还比较陌生。

解决方案

    private void updateTextView() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    String legalForm = prefs.getString("legal_form", "1");
    boolean industrialTax = prefs.getBoolean("industrial_tax", false);
    boolean turnoverTax = prefs.getBoolean("turnover_tax", false);
    boolean disbursal = prefs.getBoolean("disbursal", false);
    String capitalOwnership = prefs.getString("capitalownership", "0");
    String estIncome = prefs.getString("est_income", "1");

    settingstv.setText(getString(R.string.overview_settings,
            legalForm,
            Boolean.toString(industrialTax),
            Boolean.toString(turnoverTax),
            Boolean.toString(disbursal),
            capitalOwnership,
            estIncome));

    try {
        if (!compareCurrentWithStoredVersionCode(this, prefs, "storedVersionCode")) {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("legal_form", legalForm);
            editor.putBoolean("industrial_tax", industrialTax);
            editor.putBoolean("turnover_tax", turnoverTax);
            editor.putBoolean("disbursal", disbursal);
            editor.putString("capitalownership", capitalOwnership);
            editor.putString("est_income", estIncome);
            editor.commit();
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "updateTextView()", e);
    }
}
Run Code Online (Sandbox Code Playgroud)

Lev*_*ira 5

第一次保存时,capitalownership您可能没有将其保存为字符串,因此当您尝试检索它时,它将检索一个整数,但当您尝试转换为字符串时,getString它将出现异常。

String tmp = prefs.getString("capitalownership", "0");
Run Code Online (Sandbox Code Playgroud)

尝试这样:

int tmp = prefs.getInt("capitalownership", 0);
Run Code Online (Sandbox Code Playgroud)

此外,您已经将编辑文本设置为仅输入数字,因此您不会在此处获得字符串。

编辑:

共享首选项存储为Map<String, Object>. 正如您所看到的,地图的值是 一般Object,如果您保存为 an ,Integer则需要以整数形式检索。这是相当动态的,所以如果您一次保存为 a integer,下一次保存为 a String(到同一键),您需要跟踪您保存的类型,以便可以检索它。

如果您已保存一次为Integer,然后再次保存(以同一键)为 a String,则当您检索它时,您将需要使用getString(). 如果您不确定其中有什么,可以使用 getAll() 方法将整个首选项检索为 a Map<String, Object>,然后您可以get("capitalownership")在此映射中使用,最后检查instanceof以查看它是什么类型。但这太麻烦了,您只需要确定您将以某种类型保存该密钥(资本所有权),并确保始终这样做。

所以这就是发生的事情:

1)您尝试检索与该键关联的值,Android 会为您提供一个对象,

2) 然后,Android 尝试转换为与该方法关联的值(在您的情况下为字符串)。由于该值实际上是一个整数,这就是类转换问题的来源。