我測試了一個標準的偏好教程,沒有任何問題..但在本教程中(在所有類似的例子中),首選項活動的目的是從主要活動內部開始的...Android - 首選項 - ClassCastException
我試圖複製我的應用程序裏面的簡單的測試代碼,但首活動的意圖是從列表活動中開始......(從菜單欄)
public class TrainingListActivity extends FragmentActivity implements
TrainingListFragment.Callbacks {
// .....
public void settingsActivity() {
Intent settingsActivity = new Intent(this, Preferences.class);
startActivity(settingsActivity);
}
}
,所以我說這在manifest.xml指示TrainingListActivity作爲父活動
<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>
,但在運行它時,我得到一個運行時錯誤:
android.preference.Preference不能轉換到android.preference.GenericInflater $父
這似乎在提高首選項onCreate()
方法的結束,setDefaultValues ...任何線索?
@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)
R.xml.preferences裏面是什麼? –
感謝您指出它...我添加了在崩潰前使用的preferences.xml ...再次測試它沒有PreferenceScreen(第二預製屏幕)它運行良好!所以我需要進入它..但總體首選項活動在我的projet內運行良好... – erwin