Android - 首选项 - ClassCastException [英] Android - Preferences - ClassCastException

查看:79
本文介绍了Android - 首选项 - ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了一个标准的 Preference 教程,没有任何问题......但在本教程中(在所有类似的例子中),Preferences 活动意图是从主活动中开始的......

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...

我试图在我的应用程序中复制简单的测试代码,但 Preferences 活动意图是从 List 活动中启动的...(来自菜单栏)

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);
    }
}

所以我在 manifest.xml 中添加了它,指示 TrainingListActivity 作为父活动

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 不能转换为 android.preference.GenericInflater$Parent

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

这似乎是在 Preferences onCreate() 方法的末尾提出的,在 setDefaultValues 上......有任何线索吗?

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);
}

首选项.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>

控制台日志

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)

推荐答案

你的意思是:

<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>

?

这篇关于Android - 首选项 - ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆