我正在尝试将ImageView添加到首选片段中,以便显示颜色设置的预览。我正在通过onCreateView方法访问imageview的实例,以设置测试颜色,它将显示出来。然而,只有当我不在onCreate方法中调用addPreferencesFromResource时,它才有效——这是一个问题,因为必须添加首选项。此外,如果我保留对addPreferencesFromResource的调用,但删除整个onCreateView方法,程序将运行(没有可更新的imageview的albiet)。
这两种情况下的错误都是“Content具有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
这里是带有内联片段的首选活动:
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中包含图像视图的布局
<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。任何建议都将不胜感激。