I'm doing the HelloLinearLayout tutorial, but using string resources instead of hard coding the strings directly into the XML like the tutorial. When I run the app using string resources, it crashes immediately. When I hard code the strings into the XML code, everything works fine. Any ideas as to why my app is crashing? Thanks
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>
开发者_StackOverflow社区 <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);
}
}
You can not set background color as strings. Create an XML file at res/values/colors.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>
Then use like below.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/box4color"
android:textColor="@color/translucent_red"
android:text="Hello"/>
I this this is because of the android:background="@string/box1color"
. Why because the background attribute accommodate the integer values in the form of HEXADECIMAL .But you used string resources. I think this is the problem. But I am not sure... As per my knowledge i guessed this. If this content have wrong information please excuse.
精彩评论