user762579
user762579

Reputation:

Android - Preferences - ClassCastException

I tested a standard Preference tutorial, without any problem.. but in this tutorial(in all of the similar examples), the Preferences activity intent is started from within the main activity...

I tried to duplicate the simple test code inside my app, but the Preferences activity intent is started from within a List activity... ( from the menu bar )

public class TrainingListActivity extends FragmentActivity implements
        TrainingListFragment.Callbacks {

    // .....
    public void settingsActivity() {
        Intent settingsActivity = new Intent(this, Preferences.class);
        startActivity(settingsActivity);
    }
}

so I added it in the manifest.xml indicating TrainingListActivity as parent activity

<activity
    android:name="com.swimtechtest.swimmer.TrainingListActivity"
    android:label="@string/app_name" >
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.swimtechtest.swimmer.Preferences"
    android:label="@string/title_preferences"
    android:parentActivityName=".Preferences" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".TrainingListActivity" />
</activity>

but when running it, I get a runtime error :

android.preference.Preference cannot be cast to android.preference.GenericInflater$Parent

this seems to be raised at the end of the Preferences onCreate() method, on the setDefaultValues... any clue ?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction()
        .replace(android.R.id.content, new PrefsFragment()).commit();
    PreferenceManager.setDefaultValues(Preferences.this, R.xml.preferences,
        false);
}

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="My list of Preferences" >
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="checkboxPref"
        android:summary="This preference can be true or false"
        android:title="Checkbox Preference" />
    <ListPreference
        android:defaultValue="digiGreen"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues"
        android:key="listPref"
        android:summary="This preference allows to select an item in a array"
        android:title="Your favorite Pet" />
    <EditTextPreference
        android:name="EditText Preference"
        android:defaultValue="Nothing"
        android:key="editTextPref"
        android:summary="This allows you to enter a string"
        android:title="Edit This Text" />
    <RingtonePreference
        android:name="Ringtone Preference"
        android:key="ringtonePref"
        android:summary="Select a ringtone"
        android:title="Ringtones" />
    <PreferenceScreen
        android:key="SecondPrefScreen"
        android:summary="This is a sub PreferenceScreen"
        android:title="Secondary Level" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.preferencesexample.Preferences2"
            android:targetPackage="com.example.preferencesexample" />
    </PreferenceScreen>
    <Preference
        android:key="customPref"
        android:summary="This works almost like a button"
        android:title="Custom Preference" />
</PreferenceCategory>
</PreferenceScreen>

consolelog

FATAL EXCEPTION: main
 Process: com.swimtechtest.swimmer, PID: 9690
 java.lang.RuntimeException: Unable to start activity    
  ComponentInfo{com.swimtechtest.swimmer/com.swimtechtest.swimmer.Preferences}:  
  java.lang.ClassCastException: android.preference.Preference cannot be cast to 
   android.preference.GenericInflater$Parent
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
     ....
     Caused by: java.lang.ClassCastException: android.preference.Preference cannot be cast to android.preference.GenericInflater$Parent
at android.preference.GenericInflater.inflate(GenericInflater.java:320)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:485)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
at com.swimtechtest.swimmer.Preferences.onCreate(Preferences.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

Upvotes: 4

Views: 3903

Answers (1)

Mr_and_Mrs_D
Mr_and_Mrs_D

Reputation: 34016

Do you mean :

<activity
    android:name="com.swimtechtest.swimmer.Preferences"
    android:label="@string/title_preferences"
    android:parentActivityName="com.swimtechtest.swimmer.TrainingListActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".TrainingListActivity" />
</activity>

?

Upvotes: 1

Related Questions