【问题标题】:Adding a layout to a Viewgroup.将布局添加到视图组。
【发布时间】:2026-01-04 18:45:02
【问题描述】:

我的问题是以下我正在尝试将布局添加到视图组我的代码如下所示:

View Dyn =  (View) findViewById(R.id.lca1);

ViewGroup Layout1 = (ViewGroup) findViewById(R.id.l1);

View inflatedView = View.inflate(this, R.id.lcar1, null);
Layout1.addView(inflatedView);

我的 Logcat:

08-05 19:01:02.019: 错误/AndroidRuntime(26934): 致命异常: main 08-05 19:01:02.019: 错误/AndroidRuntime(26934): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.test/com.test.ViewFlipperExample}: android.content.res.Resources$NotFoundException: 资源ID #0x7f05002a 类型 #0x12 无效 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.os.Handler.dispatchMessage(Handler.java:99) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.os.Looper.loop(Looper.java:123) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 java.lang.reflect.Method.invokeNative(Native Method) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 java.lang.reflect.Method.invoke(Method.java:521) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 08-05 19:01:02.019:错误/AndroidRuntime(26934):在 dalvik.system.NativeStart.main(本机方法) 08-05 19:01:02.019:错误/AndroidRuntime(26934):原因:android.content.res.Resources$NotFoundException:资源 ID #0x7f05002a 类型 #0x12 无效 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.content.res.Resources.loadXmlResourceParser(Resources.java:1874) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.content.res.Resources.getLayout(Resources.java:731) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:318) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:276) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.view.View.inflate(View.java:8667) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 com.test.ViewFlipperExample.onCreate(ViewFlipperExample.java:66) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 08-05 19:01:02.019: 错误/AndroidRuntime(26934): ... 11 更多

对此的任何帮助都会有所帮助。 谢谢!!!!!!!

【问题讨论】:

    标签: android layout view viewgroup


    【解决方案1】:

    View inflatedView = View.inflate(this, R.id.lcar1, null);错了
    您应该从 res/layout 文件夹(例如 R.layout.lcar1 或任何其他)扩展布局

    【讨论】: