2011-08-13 4 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 친구를 닫지 않았습니다. 확인하십시오. –

답변

1

나는 이것이 android:background="@string/box1color" 때문입니다. 이유는 배경 속성이 HEXADECIMAL 형식의 정수 값을 수용하기 때문입니다. 그러나 문자열 리소스를 사용했습니다. 나는 이것이 문제라고 생각한다. 그러나 나는 확실하지 않다 ... 나의 지식에 따라 나는 이것을 짐작했다. 이 콘텐츠에 잘못된 정보가있는 경우 변명하십시오.

+0

당신이 정확합니다. 주어진 XML 파일을 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> 

그런 다음 아래와 같이 사용 고해상도/값/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"/> 
관련 문제