对象:( 运行 API 8)以编程方式,我试图将以下内容膨胀到单个布局main.xml
- bg_content1.xml
- bg_content2.xml
- @+id/img_logo
- @+id/str_username
笔记:
- @+id/img_logo 是位于 bg_content3.xml 中的 ImageView 的 id
- @+id/str_username 是位于 bg_content4.xml 中的 TextView 的 id
- 请不要问我为什么必须将多个布局和对象膨胀到另一个布局上。我正在尝试优化我的布局以实现可维护性。
主要的.xml
<LinearLayout android:id="@+id/main">
</LinearLayout>
bg_content1.xml
<LinearLayout android:id="@+id/bg_content1">
<TextView
android:text:"bg_content1 here"
//other attributes omitted
</TextView>
</LinearLayout>
bg_content2.xml
<LinearLayout android:id="@+id/bg_content2">
<TextView
android:text:"bg_content2 here"
//other attributes omitted
</TextView>
</LinearLayout>
bg_content3.xml
<LinearLayout android:id="@+id/bg_content2">
<ImageView
android:id:"@+id/img_logo"
//other attributes omitted
</TextView>
</LinearLayout>
bg_content4.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);
}
日志猫
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);