gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:27 25 4
gpt4 key购买 nike

我正在做 HelloLinearLayout 教程,但使用字符串资源而不是像教程那样将字符串直接硬编码到 XML 中。当我使用字符串资源运行应用程序时,它会立即崩溃。当我将字符串硬编码到 XML 代码中时,一切正常。关于为什么我的应用程序崩溃的任何想法?谢谢

主.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);
}
}

最佳答案

您不能将背景颜色设置为字符串。在 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"/>

关于android - 为什么我的 Android 应用程序在尝试访问字符串资源时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7048731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com