我在跟踪本教程。其中,PreferenceActivity用于创建设置。但是有人提到,如果我愿意,我可以使用片段,所以我继续在一个正常的活动中使用PreferenceFrament。但是当我运行这个应用程序时,它崩溃了,给出了下面的日志-
07-25 10:41:02.048 7515-7515/com.example.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sunshine/com.example.sunshine.SettingsActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4744)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.preference.PreferenceFragment.ensureList(PreferenceFragment.java:350)
at android.preference.PreferenceFragment.getListView(PreferenceFragment.java:336)
at android.preference.PreferenceFragment.bindPreferences(PreferenceFragment.java:330)
at android.preference.PreferenceFragment.onActivityCreated(PreferenceFragment.java:171)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:852)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1411)
at android.app.Activity.performStart(Activity.java:5017)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4744)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)当我搜索它时,我发现这次崩溃与ListFragment有一定的关系。但我确信我没有在我的应用程序中使用它。不过,PreferenceFragment可能会这么称呼它。我不能准确地指出这里的问题。你能帮我解决吗?
以下是我使用的首选xml -
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditTextPreference
android:title="@string/pref_location_label"
android:key="@string/pref_location_key"
android:defaultValue="@string/pref_location_default"
android:inputType="text"
android:singleLine="true"
/>
</PreferenceScreen>这是SettingsActivity,还有片段-
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new SettingsFragment())
.commit();
}
}
public static class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {
public SettingsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
addPreferencesFromResource(R.xml.pref_general);
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_settings, container, false);
return rootView;
}
/**
* Attaches a listener so the summary is always updated with the preference value.
* Also fires the listener once, to initialize the summary (so it shows up before the value
* is changed.)
*/
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}
}
}还有什么是我需要改变的,才能让它变成碎片而不是活动呢?
发布于 2014-07-25 07:21:51
终于找到了。我只需要在我的片段的xml中放置一个带有android:id="@id/android:list"属性的android:id="@id/android:list"。虽然我不知道为什么我需要它,只是通过尝试和错误找到它。
发布于 2014-10-17 17:58:55
一些更多的信息,除了解决由努布。如果有人想知道为什么添加有用,这是因为在(源代码)首选项片段-> bindpreferences()中,它实际上试图在当前视图中找到一个listview (使用android.r.id.list),如果结果视图不是listview,则抛出上述异常。此列表对于显示首选项是强制性的。
发布于 2014-07-25 05:25:05
您的问题似乎就在您的activity_settings.xml中,它说您是(应该的),用以下id创建一个视图:android.R.id.list,它不是ListView检查您的activity_settings.xml,您可能会发现问题,否则发布您的XML以查看是否有什么问题。
https://stackoverflow.com/questions/24948562
复制相似问题