melvynkim
melvynkim

Reputation: 1655

Android: Inflating multiple .XML and objects onto one single .XML

Object: (Running API 8) Programmatically, I am trying to inflate the following onto a single layout, main.xml

Note:

  1. @+id/img_logo is an id of an ImageView located in bg_content3.xml
  2. @+id/str_username is an id of a TextView located in bg_content4.xml
  3. Please don't ask me why I have to inflate multiple layouts and objects onto the other layout. I am trying to optimize my layouts for maintainability purposes.

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

}

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

SOLUTION PROVIDED BY @Ikaros I have modified some minor syntactical errors below:

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

Upvotes: 3

Views: 3102

Answers (1)

type-a1pha
type-a1pha

Reputation: 1893

For example in:

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

you're trying to inflate a child of your layout and I think this is your mistake. You can try something like:

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

If you need a reference to the ImageView you can refer to it as:

(ImageView) bg_img = (ImageView) linear_layout.findViewById(R.id.bg_img);

Upvotes: 2

Related Questions