2012-08-26 10 views
3

對象: (運行API 8) 編程,我試圖膨脹以下到一個單一的佈局,main.xml中機器人:充氣多個.XML和對象到一個單一的.XML

  • bg_c​​ontent1.xml
  • bg_c​​ontent2.xml
  • @ + ID/img_logo
  • @ + ID/str_username

注:

  1. @ + ID/img_logo是位於bg_content3.xml一個的ImageView的一個id
  2. @ + ID/str_username是位於bg_content4.xml
  3. 一個TextView的一個id
  4. 請不要問我爲什麼要將多個佈局和對象充氣到另一個佈局上。我正在嘗試優化我的佈局以實現可維護性目的。

main.xml中

<LinearLayout android:id="@+id/main"> 
</LinearLayout> 

bg_c​​ontent1.xml

<LinearLayout android:id="@+id/bg_content1"> 
    <TextView 
     android:text:"bg_content1 here" 
     //other attributes omitted 
    </TextView> 
</LinearLayout> 

bg_c​​ontent2.xml

<LinearLayout android:id="@+id/bg_content2"> 
    <TextView 
     android:text:"bg_content2 here" 
     //other attributes omitted 
    </TextView> 
</LinearLayout> 

bg_c​​ontent3.xml

<LinearLayout android:id="@+id/bg_content2"> 
    <ImageView 
     android:id:"@+id/img_logo" 
     //other attributes omitted 
    </TextView> 
</LinearLayout> 

bg_c​​ontent4.xml

<LinearLayout android:id="@+id/bg_content4"> 
    <TextView 
     android:id:"@+id/str_username" 
     //other attributes omitted 
    </TextView> 
</LinearLayout> 

MainActivity.xml

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    LinearLayout container = (LinearLayout) findViewById(R.id.main); 
    LayoutInflater inflater = LayoutInflater.from(this); 
    LinearLayout bg_content = (LinearLayout) inflater.inflate(
     R.layout.bg_content1, null); 
    container.addView(bg_content); 

    bg_content = (LinearLayout) inflater.inflate(
     R.layout.bg_content2, null); 
    container.addView(bg_content); 

    ImageView bg_img = (ImageView) inflater.inflate(R.id.img_logo, null); 
    container.addView(bg_img); 

    TextView bg_str = (TextView) inflater.inflate(R.id.str_username, null); 
    container.addView(bg_str); 

}

logcat的

08-26 14:16:43.457: D/AndroidRuntime(1726): Shutting down VM 
08-26 14:16:43.457: W/dalvikvm(1726): threadid=1: thread exiting with uncaught exception (group=0x40015578) 
08-26 14:16:43.558: E/AndroidRuntime(1726): FATAL EXCEPTION: main 
08-26 14:16:43.558: E/AndroidRuntime(1726): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.ExLayoutInflaterActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f050000 type #0x12 is not valid 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.os.Looper.loop(Looper.java:130) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at dalvik.system.NativeStart.main(Native Method) 
08-26 14:16:43.558: E/AndroidRuntime(1726): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050000 type #0x12 is not valid 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.content.res.Resources.getLayout(Resources.java:731) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.view.LayoutInflater.inflate(LayoutInflater.java:318) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at com.example.android.ExLayoutInflaterActivity.onCreate(ExLayoutInflaterActivity.java:26) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
08-26 14:16:43.558: E/AndroidRuntime(1726):  ... 11 more 

解決方案提供了一種通過@Ikaros 我已經修改了下面一些小的語法錯誤:

bg_content = (LinearLayout) inflater.inflate(
      R.layout.bg_content3, null); 
container.addView(bg_content); 

ImageView bg_img = (ImageView) bg_content.findViewById(R.id.img_logo); 

bg_content = (LinearLayout) inflater.inflate(R.layout.bg_content4, null); 
    container.addView(bg_content); 

TextView bg_str = (TextView) bg_content.findViewById(R.id.str_username); 
+0

我知道你編程的說,但存在標籤中的XML允許您重複使用佈局 – Patrick

回答

2

例如在:

ImageView bg_img = (ImageView) inflater.inflate(R.id.img_logo, null); 
container.addView(bg_img); 

你試圖誇大你的佈局的孩子,我認爲這是你r錯誤。 您可以嘗試類似:

LinearLayout linear_layout = (LinearLayout) inflater.inflate(R.layout.bg_content3.xml, null); 
container.addView(linear_layout); 

如果您需要將ImageView的你可以參考它作爲一個參考:

(ImageView) bg_img = (ImageView) linear_layout.findViewById(R.id.bg_img); 
相關問題