2011-08-13 1 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

と短い<!/ LinearLayout> '終了タグはありますか? – dustmachine

+0

親LinearLayout buddyを閉じていませんでした。 –

答えて

1

これはandroid:background="@string/box1color"のためです。背景属性がHEXADECIMALの形式で整数値を格納するためです。しかし、文字列リソースを使用しました。私はこれが問題だと思う。しかし私は確信していません...私の知る限り、私はこれを推測しました。このコンテンツに間違った情報がある場合は申し訳ありません。

+0

あなたは正しいです。 android.view.InflateException:バイナリXMLファイル行#5: "@ string/box1color"を含む行であるのエラーが発生しました 何らかの理由により、16進値を次のように変換しようとします。ドロアブル –

4

背景色を文字列として設定することはできません。

<?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> 

が続いて以下のように使うのres /値/ colors.xmlでXMLファイルを作成します。

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