【问题标题】:Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate从 onCreate 调用 addPreferencesFromResource 时在 PreferenceFragment 中使用 onCreateView 的奇怪错误
【发布时间】:2016-01-14 11:12:43
【问题描述】:

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

两种情况下的错误都是“内容具有 id 属性为 'android.R.id.list' 的视图不是 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错误。任何建议将不胜感激。

【问题讨论】:

    标签: android android-fragments android-preferences


    【解决方案1】:

    当您为 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>
    

    【讨论】:

    • 哦,太棒了。谢谢Doctoror Drive!如果 android 页面在他们通过代码与您交谈时实际提到了这一点,那将非常有帮助。
    • @Alex 找不到它,但这个异常是不言自明的。此外,如果您查看源代码,就会变得很明显,因为PreferenceScreen 膨胀为ListView,而PreferenceFragment 通过android.R.id.content 查找它。
    • 感谢收看!
    【解决方案2】:

    我今天也遇到了这个问题。它源于在这里使用 Android 教程: http://developer.android.com/guide/topics/ui/settings.html#Fragment

    基本上,我发现问题来自使用preferences.xml 文件:

    addPreferencesFromResource(R.xml.preferences);
    

    我将其注释掉后,错误就消失了,我的活动开始显示,尽管没有首选项。我会尝试追捕并在这里跟进。

    我希望到目前为止这对您有所帮助!抛出的异常不是很有帮助。

    【讨论】:

    • 为什么投反对票?我遇到了这个确切的问题,这就是导致它的问题。
    • 我猜是因为这是一种解决方法,因为它会阻止显示偏好 - 不是解决方案?
    【解决方案3】:

    我遇到了同样的问题,解决方法是确保我导入了正确的 Fragment 库:

    import android.app.Fragment;
    

    (不是支持库中的版本)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2017-08-20
      • 2012-12-28
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多