安卓include标签-无效的布局引用

43

我在Android布局XML文件中使用include标签时遇到了问题。当指定布局引用(@layout/...)时,在Eclipse ADT中会出现InflateException错误,错误如下: InflateException:您必须指定有效的布局引用。布局ID @layout/func_edit_simple_calculator_toolbox 无效。

参考应该是有效的,因为我从我的其他布局列表中选择了它而没有手动输入。我正在使用Android SDK v2.1

这些是布局文件

func_edit_simple_calculator_toolbox.xml

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

<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"android:layout_height="wrap_content">
<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"></Button>
<Button android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3"></Button>
<Button android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+"></Button>
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="@+id/Button05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4"></Button>
<Button android:id="@+id/Button06" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5"></Button>
<Button android:id="@+id/Button07" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6"></Button>
<Button android:id="@+id/Button08" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-"></Button>
</TableRow>
</TableLayout>

function_editor_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<com.calculoid.FunctionView android:id="@+id/function_view" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

<include android:id="@+id/include01" android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/func_edit_simple_calculator_toolbox"></include>
</LinearLayout>

有人知道问题可能是什么吗?

提前感谢。


1
清理项目,或将文件导入新项目。确保xml文件在正确的文件夹中。 - Pentium10
8个回答

116

如果有人在阅读这个帖子时遇到了这个错误:

Caused by: android.view.InflateException: You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />

请确保你使用:

<include layout="@layout/your_layout" />

不是:

<include android:layout="@layout/your_layout" />

layout前面不应该有android:


4
因为没有仔细阅读错误信息(或者是太过依赖自动补全),这就是我应得的后果。 - s.co.tt
1
@Mike 怎么了?在问题发布时,ggomeze的回答是解决方案。 - Dalibor Frivaldsky
完美的答案,谢谢! - Ajji
我忘了在我的上面加上'@'。 - Pamela Sillah

106

我认为我应该提一下我是如何解决同样的问题的。尝试清理项目,但没有起作用。我为布局使用了相当长的名称(chrono_first_small_field),但这也不是原因。

关闭Eclipse,然后再次打开它,这样就可以解决问题了。

这比实现onMeasure方法更有意义:-)


通过关闭Eclipse并清理项目,它成功了。谢谢。 - AlikElzin-kilaka
当我进行包含时遇到了相同的问题,这一定是与Eclipse/Android插件兼容性有关的错误。 - Blundell
需要重新启动 Eclipse / ADB 的 BUG 之一 :D。 - Ovidiu Latcu
我很难过,这个错误占用了我两个小时的时间,而我只需要做这个。 - DeeV
1小时的浪费在一分钟内解决了。 - Shahzad Imam
哇,Eclipse 真的有一些问题。首先我几乎在使用 WindowBuilder 为一些 Java 应用程序而失去理智,现在又出了这种问题。天啊。 - c0dehunter

1

我没有立即看到任何问题,所以这有点奇怪。我只能想到两件事情:1)您是否尝试使用较短的文件名?文件名中有一些限制,例如不能有大写字符或符号,以确保兼容性,文件名长度可能是其中之一。2)您是否尝试删除包含中的一些android:...语句,并将其留在<include layout="@layout/..."/>?实际上,在包含中不需要指定其高度和宽度,因为这已经在导入的布局文件中定义了。


我真的不太确定是什么“解决”了它,但它似乎现在至少可以工作。我只是尝试不同的布局设置。另外一件事是,我已经用一个简单的主体覆盖了自定义视图中的onMeasure(int,int)方法(之前,FunctionView仅包含自动生成的构造函数)。无论如何,感谢您的建议。 - Dalibor Frivaldsky
嗯,我很高兴你已经解决了你的问题,不管你是怎么做到的!但是,你真的不应该有一个空的onMeasure(int, int)函数 - 如果你的布局变得更加复杂,我想这将开始破坏事情。此外,如果你愿意,还有一些“文书工作”需要完成。Stack Overflow通过问题是否有“被接受的答案”来跟踪问题是否已经解决。目前,这个问题仍然会出现在未解决的问题列表中 - 你可以接受这个答案,或者 - - Steve Haley
或者编写自己的答案,即使它只是说将它们指向阅读您的评论的点?这只是保持网站整洁的问题。 - Steve Haley
不,onMeasure(int,int)并不是空的,在派生类中根本没有定义它(正如我所说,只定义了构造函数),因此View类的实现已经为其完成了工作。我将关闭这个问题。 - Dalibor Frivaldsky

0

检查是否为空

<include>
</include>

标签,(可能是由于自动代码格式化引起的)。

它们可能会出现在您的xml中的某个“隐藏”位置。


0
对我来说,问题在于布局的名称错误:我使用了“-”而不是“_”。AndroidStudio直到我尝试部署应用程序才告诉我有问题。

0

我已经“解决”了问题,但我不知道到底是什么起了作用。我的布局文件并没有经过任何重大的修改,基本上还是一样的。在这个问题消失之前,我唯一值得注意的更改是在我的自定义视图类中覆盖了 onMeasure(int,int) 方法,具体实现只是调用了 setMeasuredDimension(100,100)。


0
针对使用Xamarin Studio的用户 - 我从另一个项目中添加了一堆AXML文件,也遇到了完全相同的错误。我只是简单地关闭并重新启动了Xamarin Studio (PC),这也解决了问题。有趣的是,不同的IDE也存在相同的问题。

0

使用-clean选项启动Eclipse,您的问题将得到保证地解决。不过启动时间会比平常多30秒左右。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接