O
O
Oleksandr Shcherbak2019-08-30 20:18:44
Java
Oleksandr Shcherbak, 2019-08-30 20:18:44

What's wrong with SettingsFragment?

For some unknown reason, today the application on the emulator began to fall out. Did three weeks ago, everything worked. Right now I'm just fine-tuning it. The error appears when accessing the settings. And on the emulator with android 5.1 everything works, but on the emulator with android 7.0 it falls out. Why can you help me figure it out?
5d69591a507d6045712751.jpeg
SettingsActivity code:

import android.os.Bundle;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }
}

SettingsFragment code
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }
}

Code PreferenceScreen
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
        android:icon="@drawable/ic_text"
        android:defaultValue="3"
        android:entries="@array/Set_string"
        android:entryValues="@array/Set_Values"
        android:summary="%s"
        android:key="size"
        android:title="@string/Сhoose_Size">
    </ListPreference>

    <Preference
        android:icon="@drawable/ic_info"
        android:title="Сайт Свято-Покровської Церкви"
        android:summary="pokrova.pl.ua" >
        <intent
            android:action="android.intent.action.VIEW"
            android:data="@string/url_site" />
    </Preference>

</PreferenceScreen>

string-array
<string name="url_site">https://pokrova.pl.ua</string>

    <string name="size" translatable="false">size</string>
    <string-array name="Set_string">
        <item>14</item>
        <item>16</item>
        <item>18</item>
        <item>20</item>
        <item>22</item>
        <item>24</item>
        <item>26</item>
    </string-array>
    <string-array name="Set_Values">
        <item>14</item>
        <item>16</item>
        <item>18</item>
        <item>20</item>
        <item>22</item>
        <item>24</item>
        <item>26</item>
    </string-array>

Error code
--------- beginning of crash
2019-08-30 19:48:30.490 1055-1055/ua.pl.prayerbook E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ua.pl.prayerbook, PID: 1055
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ua.pl.prayerbook/ua.pl.prayerbook.SettingsActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
        at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:225)
        at android.support.v7.preference.Preference.getPersistedString(Preference.java:1668)
        at android.support.v7.preference.ListPreference.onSetInitialValue(ListPreference.java:260)
        at android.support.v7.preference.Preference.onSetInitialValue(Preference.java:1594)
        at android.support.v7.preference.Preference.dispatchSetInitialValue(Preference.java:1566)
        at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1293)
        at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1305)
        at android.support.v7.preference.PreferenceGroup.addPreference(PreferenceGroup.java:260)
        at android.support.v7.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:181)
        at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:363)
        at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:170)
        at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:120)
        at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:138)
        at android.support.v7.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:445)
        at ua.pl.prayerbook.SettingsFragment.onCreatePreferences(SettingsFragment.java:10)
        at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:228)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
        at android.app.Activity.performStart(Activity.java:6679)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2019-08-31
@shesa

java.lang.Integer cannot be cast to java.lang.String

For some key, SharedPreferences contains an int and you are trying to count it as a String

M
Master_T, 2019-08-31
@Master_T

And what was not specified in R.xml.preferences.
What's there?
He is most likely affective, judging by the logo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question