为什么我尝试访问字符串资源时,我的Android应用程序会崩溃?

4
我正在进行HelloLinearLayout教程,但是使用字符串资源而不是像教程中直接将字符串硬编码到XML中。当我使用字符串资源运行应用程序时,它会立即崩溃。当我将字符串硬编码到XML代码中时,一切正常工作。有什么想法可以解决我的应用程序崩溃问题吗?谢谢。 Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">
            <TextView
                android:text="@string/box1text"
                android:gravity="center_horizontal"
                android:background="@string/box1color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box1weight"
                />
            <TextView
                android:text="@string/box2text"
                android:gravity="center_horizontal"
                android:background="@string/box2color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box2weight"
                />
            <TextView
                android:text="@string/box3text"
                android:gravity="center_horizontal"
                android:background="@string/box3color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box2weight"
                />
            <TextView
                android:text="@string/box4text"
                android:gravity="center_horizontal"
                android:background="@string/box4color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box4weight"
                />
            </LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, HelloLinearLayoutActivity!</string>
        <string name="app_name">HelloLinearLayout</string>
        <string name="box1text">red</string>
        <string name="box1color">#aa0000</string>
        <string name="box1weight">1</string>
        <string name="box2text">green</string>
        <string name="box2color">#00aa00</string>
        <string name="box2weight">1</string>
        <string name="box3text">blue</string>
        <string name="box3color">#0000aa</string>
        <string name="box3weight">1</string>
        <string name="box4text">yellow</string>
        <string name="box4color">#aaaa00</string>
        <string name="box4weight">1</string>
    </resources>

hellolinearlayoutactivity.java

package com.example.hellolinearlayout;

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

public class HelloLinearLayoutActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

你能贴出崩溃的logcat输出吗? - Nick Rempel
你是否缺少一个 </LinearLayout> 的闭合标签? - dustmachine
你没有关闭你的父级LinearLayout,伙计。检查一下。 - Android Killer
2个回答

4
您不能将背景颜色设置为字符串。 请在res/values/colors.xml中创建一个XML文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
  <color name="box4color">#aaaa00</color>
</resources>

然后按以下方式使用。
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/box4color"
    android:textColor="@color/translucent_red"
    android:text="Hello"/>

1

我认为这是因为 android:background="@string/box1color"。因为背景属性需要以十六进制的形式容纳整数值,但你使用了字符串资源。我认为这就是问题所在,但我不确定……根据我的知识,我猜测是这个原因。如果这个内容有错误,请谅解。


你是正确的。直接使用给定的XML文件会导致android.view.InflateException: Binary XML file line #5: Error inflating class <unknown>,其中包含"@string/box1color"的那一行。由于某种原因,它试图将十六进制值转换为可绘制对象。 - Brandon Haugen

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