我正在尝试将
ImageView添加到首选项片段,以显示颜色设置的预览.我正在通过onCreateView方法访问imageview的实例来设置测试颜色,它将显示.但是它只有在我没有在onCreate方法中调用addPreferencesFromResource时才有效 – 这是一个问题,因为必须添加首选项.此外,如果我将调用留给addPreferencesFromResource,但删除整个onCreateView方法,程序将运行(没有可更新的imageview的albiet).
两种情况下的错误都是“内容具有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
这里是带有内联片段的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,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.这几乎看起来像一个机器人的bug.任何建议将不胜感激.
解决方法
当您为PreferenceActivity或PreferenceFragment创建自定义layot时,您必须提供一个ID为android.R.id.list的ListView,其中Preferences将转到.
<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>