java 错误:不允许字符串类型(在 'layout_height' 处,值为 'wrap content')。活动_main.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17641307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:33:26  来源:igfitidea点击:

Error: String types not allowed (at 'layout_height' with value 'wrap content'). activity_main.xml

javaandroidxml

提问by TheOrangeRemix

I am new to the forum and just started learning about Android app development. I was going through the tutorials at developer.android.com and already ran into an compiling error. I see these three errors on my activity_main.xml:

我是论坛的新手,刚刚开始学习 Android 应用程序开发。我正在浏览 developer.android.com 上的教程,但已经遇到了编译错误。我在 activity_main.xml 上看到这三个错误:

error: Error: String types not allowed (at 'layout_height' with value 'wrap content').  activity_main.xml   
error: Error: String types not allowed (at 'layout_width' with value 'wrap content').   activity_main.xml   
error: Error: String types not allowed (at 'layout_height' with value 'wrap content').  activity_main.xml

and here is my activity_main.xml code as of now:

这是我目前的activity_main.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="horizontal"
    tools:context=".MainActivity" >

    <EditText android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap content"
        android:layout_weight="1"
        android:hint="@string/edit_message" />

    <Button
        android:layout_width="wrap content"
        android:layout_height="wrap content"
        android:text="@string/button_send" />

</LinearLayout>

Is it throwing an error because wrap_content is an incorrect syntax or does it need to be declared elsewhere? Any help will be appreciated on how to troubleshoot or fix this.

是因为 wrap_content 的语法不正确还是需要在其他地方声明而引发错误?任何有关如何排除故障或解决此问题的帮助将不胜感激。

Thanks!

谢谢!

回答by Glenn

I've just started learning myself. I believe the problem is missing underscores. It should read "wrap_content" not "wrap content".

我自己才刚刚开始学习。我相信问题是缺少下划线。它应该读为“wrap_content”而不是“wrap content”。

回答by Umer Farooq

It's wrap_contentnot wrap content. Change it and your problem should be long gone!

这是wrap_content不是wrap content。改变它,你的问题应该早就不复存在了!

回答by J W

I was getting the same error while evaluating Xamarin on Mac. It was an issue with target framework. My guess is that certain earlier versions of Android did not support all values in this Xml tag, or Xamarin could not handle it properly, or something like that. I changed Options->Build->General->Target framework from Android 2.1 to Android 2.2, and the build was successful after that.

我在 Mac 上评估 Xamarin 时遇到了同样的错误。这是目标框架的问题。我的猜测是某些早期版本的 Android 不支持此 Xml 标记中的所有值,或者 Xamarin 无法正确处理它,或者类似的问题。我把Options->Build->General->Target framework从Android 2.1改成Android 2.2,之后构建成功。

回答by Tarsem Singh

it must bewrap_contentinstead ofwrap content

必须是wrap_content而不是wrap content

Change

改变

 <EditText android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap content"
        android:layout_weight="1"
        android:hint="@string/edit_message" />

    <Button
        android:layout_width="wrap content"
        android:layout_height="wrap content"
        android:text="@string/button_send" />

To

 <EditText android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_message" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />

回答by Micro

I was typing in "warp_content" and it was messing up for me lol. It's "wrap_content" not "warp_content"

我正在输入“warp_content”,这对我来说很糟糕,哈哈。它是“wrap_content”而不是“warp_content”