android - 从 onCreate 调用 addPreferencesFromResource 时在 PreferenceFragment 中使用 onCreateView 的奇怪错误

标签 android android-fragments android-preferences

我正在尝试将 ImageView 添加到首选项 fragment 以显示颜色设置的预览。我正在通过 onCreateView 方法访问 imageview 的实例以设置测试颜色,它将显示。但是,它只有在我不在 onCreate 方法中调用 addPreferencesFromResource 时才有效——这是一个问题,因为必须添加首选项。此外,如果我保留对 addPreferencesFromResource 的调用,但删除整个 onCreateView 方法,程序将运行(尽管没有可更新的 ImageView )。

两种情况下的错误都是“内容具有 id 属性为‘android.R.id.list’的 View ,它不是 ListView 类”

我曾尝试从 onCreate 访问 imageview,但此时布局项已膨胀,我似乎无法访问显示的实际实例。

LogCat 错误:

04-11 00:42:43.619: E/AndroidRuntime(5362): FATAL EXCEPTION: main
04-11 00:42:43.619: E/AndroidRuntime(5362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.signalwidget/com.example.android.signalwidget.SignalWidgetConfigure}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class

这里是带有内联 fragment 的 PreferenceActivity:

public class SigConfigure extends PreferenceActivity {

private static int prefs=R.xml.pref_widget_colors;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getFragmentManager().beginTransaction().replace(android.R.id.content, new ColorsFragment()).commit();

}

@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.pref_headers, target);
}

public static class ColorsFragment extends PreferenceFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(SignalWidgetConfigure.prefs);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

        //just testing to see if the imageview can be accessed.
        View v = inflater.inflate(R.layout.layout_pref_row, container, false);
        ImageView iv = (ImageView) v.findViewById(R.id.color_preview);
        iv.setBackgroundColor(Color.CYAN);

        return v;
    }


}}

这里是pref_widget_colors中的偏好定义

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

    <Preference
        android:key="wifi_signal_color"
        android:title="WiFi Signal Color" >
        <intent
            android:action="android.intent.action.VIEW"
             android:targetClass="com.example.android.signalwidget.colorpicker.PickerActivity"
            android:targetPackage="com.example.android.signalwidget" />
    </Preference>
    <Preference
        android:key="cell_signal_color"
        android:title="Cell Signal Color" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.android.signalwidget.colorpicker.PickerActivity"
            android:targetPackage="com.example.android.signalwidget" />
    </Preference>

</PreferenceScreen>

这是在 layout_pref_row.xml 中包含 ImageView 的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/color_preview"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="5dp"
        android:background="#aaa" />
</LinearLayout>

尽管出现错误,但我没有在我的项目中的任何地方使用 ListView 或 ListFragment。这几乎看起来像一个 android 错误。任何建议将不胜感激。

最佳答案

当您为 PreferenceActivity 或 PreferenceFragment 创建自定义布局时,您必须提供 ID 为 android.R.id.listListView首选项的位置。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/color_preview"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="5dp"
        android:background="#aaaaaa" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp" />

</LinearLayout>

关于android - 从 onCreate 调用 addPreferencesFromResource 时在 PreferenceFragment 中使用 onCreateView 的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15941214/

相关文章:

java - EditText默认选择?

android - 如果来自服务器的响应在android中为空,如何处理?

android - 如何在eclipse模拟器中获取内置日历应用程序的源代码?

android - OpenCV Android Kmeans 无法按预期工作

android - 在 fragment 中使用FragmentManager?

android - 写入共享首选项太慢?

android - 不支持使用 FragmentTransaction 添加 ListFragment?

android - 当您必须使用父 Activity 引用时,如何使用 `FragmentScenario` 单独测试 Fragment

android - 如何获取 Android 首选项的类别?

android - 如何在PreferenceFragmentCompat中实现RingtonePreference?