【问题标题】:Using include as root layout node throws "Error inflating class include" exception使用 include 作为根布局节点会引发“Error inflating class include”异常
【发布时间】:2012-01-11 21:50:44
【问题描述】:

我尝试根据操作系统版本使用不同的列表项布局。

因此,我创建了与条件相关的不同布局。 其中之一是(layout/search_result_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<include xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    layout="@android:layout/simple_list_item_1">
</include>

它包括标准的“simple_list_item_1”。

在我的 Java 代码中,布局与列表相关联,如下所示:

    adapter = new SimpleCursorAdapter(getActivity(),
                                      R.layout.search_results_list_item,
                                      null,
                                      from,
                                      to,
                                      0);

显示列表项时,抛出以下异常:

android.view.InflateException: Binary XML file line #2: Error inflating class include
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
   at android.support.v4.widget.ResourceCursorAdapter.newView(ResourceCursorAdapt

怎么了?不能用作根项目?尽管 ADT 允许这样做。

【问题讨论】:

  • 我不确定这一点,但是您是否尝试过将根用作线性布局,并在高度和宽度上使用 fill_parent,并在其中包含包含。
  • 是的,也许它可以工作,但我不想让我的列表视图重载无用的中间视图(每行一个)。
  • 然后不要定义你自己的xml,只需将simple_list_item_1传递给适配器而不是你的
  • 目的是根据操作系统版本使用不同的布局
  • 您找到解决方案了吗?我有同样的问题。提前致谢。 :-)

标签: android android-layout


【解决方案1】:

如果其他人想知道这就是答案:

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <include xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@android:layout/simple_list_item_1">
    </include>
</merge>

【讨论】:

【解决方案2】:

您可以使用资源文件夹上的限定符来提供特定于 Android 操作系统版本的资源。

见:http://developer.android.com/guide/topics/resources/providing-resources.html

可能的限定符列表中的最后一项是版本(API 级别)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2011-01-06
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2016-12-01
    相关资源
    最近更新 更多