从onCreate调用addPreferencesFromResource时在PreferenceFragment中使用onCreateView产生奇怪的错误 [英] Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate
问题描述
我正在尝试将ImageView添加到首选项片段中,以显示颜色设置的预览.我正在通过onCreateView方法访问imageview的实例以设置测试颜色,它将显示.但是,它仅在我未在onCreate方法中调用addPreferencesFromResource的情况下有效-这是一个问题,因为必须添加首选项.另外,如果我保留对addPreferencesFromResource的调用,但删除整个onCreateView方法,则程序将运行(没有可更新的imageview的情况).
I'm trying to add an ImageView to a preference fragment in order to show a preview of a color setting. I'm accessing the instance of the imageview via the onCreateView method toset the test color, and it will display. However it only works if I don't call addPreferencesFromResource in the onCreate method - which is a problem since the preferences must be added. Also if I leave the call to addPreferencesFromResource, but remove the entire onCreateView method the program will run (albiet without the updatable imageview).
两种情况下的错误均是内容具有ID属性为'android.R.id.list'的视图,而该属性不是ListView类"
The error in both cases is "Content has view with id attribute 'android.R.id.list' that is not a ListView class"
我试图从onCreate访问imageview,但是到那时布局项膨胀了,我似乎无法访问所显示的实际实例.
I have tried to access the imageview from onCreate, but by then the layout items are inflated and I can't seem to access the actual instance that is displayed.
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
以下是带有内联片段的PreferenceActivity:
Here the PreferenceActivity with inline Fragment:
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中的首选项定义
Here is the preference definition in 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的布局
Here is the layout containing the imageview in layout_pref_row.xml
<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错误.任何建议将不胜感激.
Despite the error I am not using a ListView or a ListFragment anywhere in my project. This almost seems like an android bug. Any suggestion would be appreciated.
推荐答案
为PreferenceActivity或PreferenceFragment创建自定义布局时,您必须必须提供ID为 ListView 的 android.R.id.list (偏好设置所在的位置).
When you create a custom layot for a PreferenceActivity or a PreferenceFragment you must supply a ListView with id android.R.id.list where the Preferences go to.
<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>
这篇关于从onCreate调用addPreferencesFromResource时在PreferenceFragment中使用onCreateView产生奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!