【问题标题】:LinearLayout and FrameLayout multiple errors in Android StudioAndroid Studio中的LinearLayout和FrameLayout多个错误
【发布时间】:2018-03-24 00:39:53
【问题描述】:

所以我目前正在制作这个应用程序,这是我正在使用/编码的 activity_maps.xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<Button
    android:id="@+id/btnRestaurant"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Nearby Restaurants"
    android:visibility="visible" />

<Button
    android:id="@+id/btnHospital"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Nearby Hospitals"
    android:visibility="visible" />

<Button
    android:id="@+id/btnSchool"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Nearby Schools"
    android:visibility="visible" />
</FrameLayout>

显示的错误是,每当我将鼠标悬停在 linearlayout 上方时,它都会显示:应定义 layout_height 属性,应定义 layout_width 属性,Element LinearLayout 没有必需的属性 layout_height,Element LinearLayout 没有必需的属性 layout_width,错误方向?没有指定方向,默认是水平的,但是这个布局有多个孩子,其中至少一个有 layout_width="match_parent"。然后这些是我将鼠标悬停在 FrameLayout 上方时的错误:应该定义 layout_height 属性,应该定义 layout_width 属性。这些是我将鼠标悬停在按钮标签上方时出现的错误:应该定义 layout_height 属性,应该定义 layout_width 属性。这些是我将鼠标悬停在 abdroid:id="@+id/btnwhatever", android:text="whatever", android:visibility="visible" 标签上方时出现的错误,它们都说:Unexpected namespace prefix "android" found for tag Button . 当我将应用程序的布局更改为:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="350dp"
android:layout_height="500dp"

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_height="125dp"
    android:layout_width="75dp">

    <LinearLayout 
        xmlns:android="https://schemas.android.com/apk/res/android"
        xmlns:tools="https://schemas.android.com/tools"
        android:layout_height="75dp"
        android:layout_width="100dp"
        android:orientation="vertical">

        <Button
            android:id="@+id/btnRestaurant"
            android:layout_height="10dp"
            android:layout_width="10dp"
            android:text="Nearby Restaurants"
            android:visibility="visible" />

        <Button
            android:id="@+id/btnHospital"
            android:layout_height="10dp"
            android:layout_width="10dp"
            android:text="Nearby Hospitals"
            android:visibility="visible" />

        <Button
            android:id="@+id/btnSchool"
            android:layout_height="10dp"
            android:layout_width="10dp"
            android:text="Nearby Schools"
            android:visibility="visible" />

    </LinearLayout>
</FrameLayout>

但是,当我执行没有显示错误的布局然后打开它在 logcat 上显示的地图时:java.lang.RuntimeException: Binary XML file line #0: You must provide a layout_width attribute 请帮忙,谢谢。 附言我刚刚取出了片段中的 tools:context="" 标签进行发布

【问题讨论】:

  • 亲爱的 Mike,我已将 /> 放在片段代码的末尾,当我运行应用程序时它会显示在 log cat 中:java.lang.RuntimeException: Binary XML file line #0 :您必须提供一个 layout_width 属性,然后应用程序就会崩溃,说:不幸的是,地图已停止工作。请帮忙,谢谢!

标签: android android-layout android-linearlayout android-framelayout


【解决方案1】:

我看到了和你一样的错误。

我发现在 FrameLayout 中,确保它是 http 而不是 https

xmlns:android="http://schemas.android.com/apk/res/android

而不是

xmlns:android="https://schemas.android.com/apk/res/android. 

只需删除“s”。它对我有用。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    正如迈克所说,并从 FrameLayout 标记中删除重复的 xmlns xmlns:android="https://schemas.android.com/apk/res/android"。这是我测试过的代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="vertical">
    
            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:map="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></fragment>
    
            <FrameLayout
                android:layout_height="match_parent"
                android:layout_width="match_parent">
    
                <Button
                    android:id="@+id/btnRestaurant"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="Nearby Restaurants"
                    android:visibility="visible" />
    
                <Button
                    android:id="@+id/btnHospital"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="Nearby Hospitals"
                    android:visibility="visible" />
    
                <Button
                    android:id="@+id/btnSchool"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="Nearby Schools"
                    android:visibility="visible" />
            </FrameLayout>
        </LinearLayout>
    

    【讨论】:

    • 1) 删除重复属性与 OP 面临的问题无关。这只是一个建议,所以最好将其发布为评论! 2)您发布的代码并不代表您要求 OP 执行的操作 3)&lt;fragment&gt; 标签可以用 /&gt; 关闭,而不是像 Mike 在评论中所说的 &gt; &lt;/fragment&gt;
    • 亲爱的 Ruben,我已将 /> 放在片段代码的末尾,当我运行应用程序时它会显示在 log cat 中:java.lang.RuntimeException: Binary XML file line #0 :您必须提供一个 layout_width 属性,然后应用程序就会崩溃,说:不幸的是,地图已停止工作。请帮忙,谢谢!
    • @SandipSoni 只有在我发布了我的测试代码后,我才看到 mike 发现了一个错误 &gt; 而不是 /&gt;。也不是&gt; &lt;/fragment&gt; 不正确。所以在发布时我的答案是没有错误的,测试总是更好,不是吗?同样在前两行我问 OP 该怎么做!
    • @TEPneil 如果你复制粘贴我的代码,它还是一样吗?您是否尝试过清理和重建项目?
    • @ruben,我复制并粘贴了你的代码,你猜怎么着?有效!现在的问题是按钮没有显示出来,但是它显示在应用程序的设计中,并且框架布局和片段非常小,但是应用程序主要显示地图并且能够显示当前位置。请帮忙,谢谢大佬
    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    相关资源
    最近更新 更多