2011-08-13 80 views
4

我在做HelloLinearLayout教程,但使用字符串資源,而不是像本教程那樣直接將字符串硬編碼到XML中。當我使用字符串資源運行應用程序時,它立即崩潰。當我將字符串硬編碼到XML代碼中時,一切正常。任何想法,爲什麼我的應用程序崩潰?由於當我嘗試訪問字符串資源時,爲什麼我的Android應用程序崩潰?

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); 
    } 
} 
+0

你可以發佈你的logcat輸出的崩潰? –

+0

並且你是一個簡短的''結束標記嗎? – dustmachine

+0

你沒有關閉你的父母LinearLayout buddy.check它。 –

回答

1

我這是因爲android:background="@string/box1color"。爲什麼因爲背景屬性容納了HEXADECIMAL形式的整數值,但是您使用了字符串資源。我認爲這是問題。但我不確定......據我所知,我猜測這一點。如果這個內容有錯誤的信息,請原諒。

+0

你是對的。以android.view.InflateException中給出的xml文件結果爲例:Binary XML file line#5:Error inflate class 這是包含「@ string/box1color」的行 由於某些原因,它試圖將十六進制值轉換爲一個可繪製的 –

4

您不能將背景顏色設置爲字符串。 創建於RES /價值/ 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> 

然後使用如下的XML文件。

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/box4color" 
    android:textColor="@color/translucent_red" 
    android:text="Hello"/> 
相關問題