gpt4 book ai didi

Android:将多个 .XML 和对象膨胀到一个 .XML

转载 作者:太空狗 更新时间:2023-10-29 13:34:28 26 4
gpt4 key购买 nike

对象:(运行 API 8)以编程方式,我试图将以下内容扩展到单个布局 main.xml

  • bg_content1.xml
  • bg_content2.xml
  • @+id/img_logo
  • @+id/str_用户名

注意:

  1. @+id/img_logo 是位于 bg_c​​ontent3.xml 中的 ImageView 的 id
  2. @+id/str_username 是位于 bg_c​​ontent4.xml 中的 TextView 的 id
  3. 请不要问我为什么必须将多个布局和对象膨胀到另一个布局上。我正在尝试优化我的布局以实现可维护性目的。

ma​​in.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);

最佳答案

例如:

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

您正试图膨胀布局的子项,我认为这是您的错误。你可以尝试这样的事情:

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);

关于Android:将多个 .XML 和对象膨胀到一个 .XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12133991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com