【问题标题】:Eclipse Android error when switching to GUI xml切换到 GUI xml 时出现 Eclipse Android 错误
【发布时间】:2012-12-28 04:26:26
【问题描述】:

当我为首选项布局文件从 xml 切换到 GUI 时出现以下错误:

渲染期间引发异常:com.android.layoutlib.bridge.MockView 无法转换为 android.view.ViewGroup 异常详细信息记录在窗口 > 显示视图 > 错误日志中 找不到以下类: - PreferenceCategory(修复构建路径,编辑 XML) - PreferenceScreen(修复构建路径,编辑 XML)

我的 xml 文件如下所示:

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

  <PreferenceCategory
    android:title="Activation">
    <CheckBoxPreference
      android:title="Title1"
      android:defaultValue="false"
      android:key="checkbox1">
    </CheckBoxPreference>
  </PreferenceCategory>

  <PreferenceCategory
    android:title="Option1">
    <PreferenceScreen
        android:title="Set Option">
    <CheckBoxPreference
      android:title="ENABLE OPTION"
      android:defaultValue="false"
      android:summary="text"
      android:key="checkboxOption1">
    </CheckBoxPreference>
    <CheckBoxPreference
      android:title="DISPLAY OPTIONS"
      android:defaultValue="false"
      android:summary="text"
      android:key="checkboxDisplay1">
    </CheckBoxPreference>
    <EditTextPreference
      android:title="OPTION2"
      android:name="Option2"
      android:summary="text"
      android:defaultValue="1"
      android:key="editOption2">
    </EditTextPreference>
    <CheckBoxPreference
      android:title="OPTION3"
      android:defaultValue="false"
      android:summary="text"
      android:key="checkboxOption3">
    </CheckBoxPreference>
    </PreferenceScreen>
  </PreferenceCategory>

  <PreferenceCategory
    android:title="Option4">
    <PreferenceScreen
        android:title="Set Option4">
    <CheckBoxPreference
      android:title="OPTION4"
      android:defaultValue="false"
      android:summary=""
      android:key="checkboxOption4">
    </CheckBoxPreference>
    <CheckBoxPreference
      android:title="OPTION5"
      android:defaultValue="false"
      android:summary="text"
      android:key="checkboxOption5">
    </CheckBoxPreference>
    <EditTextPreference
      android:title="OPTION6"
      android:name="Option6"
      android:summary="text"
      android:defaultValue="1"
      android:key="editOption6">
    </EditTextPreference>
    <EditTextPreference
      android:title="OPTION7"
      android:name="Option7"
      android:summary="text"
      android:defaultValue="1"
      android:key="editOption7">
    </EditTextPreference>
    <EditTextPreference
      android:title="OPTION8"
      android:name="Option8"
      android:summary="text"
      android:defaultValue="1"
      android:key="editOption8">
    </EditTextPreference>
    </PreferenceScreen>
  </PreferenceCategory>

  <PreferenceCategory
    android:title="OPTION9">
    <PreferenceScreen
        android:title="Option9">
    <EditTextPreference
      android:title="Option9"
      android:name="Option9"
      android:summary="text"
      android:defaultValue=""
      android:key="editOption9">
    </EditTextPreference>
    </PreferenceScreen>    
  </PreferenceCategory>

</PreferenceScreen>

任何帮助将不胜感激

【问题讨论】:

  • 我也有同样的问题。

标签: android-layout


【解决方案1】:

我遇到了这个问题,因为我将偏好列表视为正常活动。它实际上是完全不同的。首先,您需要将 XML 文件从 res/layout 移动到 res/xml(在 Eclipse 中,您必须手动创建此文件夹)。您还必须使用不同的 Java 代码:

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity { //NOT activity!

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

        // Initialize the preference screen defined in /res/xml/preference.xml
        addPreferencesFromResource(R.xml.preferences); //NOT setContentView
    }

}

您将收到关于 API 级别 >=11 的警告,说明 addPreferencesFromResource 已被弃用。这是因为 Android 希望您切换到基于片段(“PreferenceFragments”)的偏好屏幕,这有点复杂。有一个例子here

【讨论】:

  • 谢谢 1" 会检查一下!干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 2012-07-08
相关资源
最近更新 更多