【问题标题】:Why does this app crash with a Runtime error?为什么这个应用程序会因运行时错误而崩溃?
【发布时间】:2019-02-01 05:21:42
【问题描述】:

我有一个 android 项目,它主要是来自 android studio 的模板应用程序。这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/gameStartButton"
        android:text="Start"
        android:fontFamily="serif"
        android:textSize="30sp"
        android:textColor="@color/colorAccent"
        android:background="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="@string/mainMenuItemWidth"/>
</android.support.constraint.ConstraintLayout>

这是MainActivity.java: 包 com.example.saga.test;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

当我启动这个应用程序时,它会因以下回溯而崩溃:

08-26 17:57:36.256 5868-5868/com.example.saga.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.saga.test, PID: 5868
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.saga.test/com.example.saga.test.MainActivity}: java.lang.RuntimeException: Binary XML file line #0: You must supply a layout_height attribute.
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)

如您所见,我实际上为约束布局和按钮提供了layout_height 属性,为什么会出现此错误?

【问题讨论】:

  • 它告诉你哪个是解决方案:You must supply a layout_height attribute.

标签: android xml android-activity


【解决方案1】:

您不能对 layout_height 使用字符串值。将值放入dimens.xml 并使用

引用该值
android:layout_height="@dimen/mainMenuItemWidth"

【讨论】:

    【解决方案2】:

    根本原因:您正在为属性android:layout_height 使用字符串。预期是 integerdimen 值。

    解决方案:res/values 文件夹中创建一个名为dimens.xml 的新文件,然后将以下部分添加到其中。

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="mainMenuItemWidth">20dp</dimen>
    </resources>
    

    然后在你的 xml 文件中。

    <Button
            android:id="@+id/gameStartButton"
            android:text="Start"
            android:fontFamily="serif"
            android:textSize="30sp"
            android:textColor="@color/colorAccent"
            android:background="@color/colorPrimaryDark"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="@dimen/mainMenuItemWidth"/>
    

    【讨论】:

      【解决方案3】:

      应用程序因android:layout_height="@string/mainMenuItemWidth" 而崩溃,因为layout_height 不接受字符串类型值。

      dimen 文件中创建一个名为mainMenuItemWidth 的值

      android:layout_height="@string/mainMenuItemWidth" 更改为android:layout_height="@dimen/mainMenuItemWidth"

      我希望它对你有用。

      【讨论】:

        猜你喜欢
        • 2015-02-12
        • 2011-05-21
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-01
        相关资源
        最近更新 更多