0

Basically I created a separate XML containing a linear layout which contains a image and a textview, and I need this linear layout inside of another linear layout, where I need to add my first linear layout dynamically to the second linear layout.

First linear layout, a separate XML file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/storeLocation"
        android:layout_width="@dimen/settings_item_width"
        android:layout_height="@dimen/settings_item_height"
        android:layout_marginBottom="10dp"
        android:visibility="@integer/sales_order_settings_visibility"
        style="@style/dashboard_btn"
        android:onClick="onStoreLocationClick"
        android:clickable="true"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="@dimen/settings_item_image_container_width"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageView
                android:layout_width="@dimen/setting_item_image_size"
                android:layout_height="@dimen/setting_item_image_size"
                android:tint="@color/cherry_red_without_opacity"
                android:src="@drawable/ic_location_icon" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="40dp"
            android:gravity="center|left">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Store Location"
                android:textColor="@color/white"
                android:textSize="@dimen/top_section_purchase_id_font" />

        </LinearLayout>
    </LinearLayout>


</LinearLayout>

Second linear layout, separate XML:

<LinearLayout
                            android:id="@+id/settingButtonHolder"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:orientation="vertical">
</LinearLayout>

Android Studio code:

linearLayout = (LinearLayout) findViewById(R.id.settingButtonHolder);
            linearLayout2 = (LinearLayout) findViewById(R.id.storeLocation);
            linearLayout.addView(linearLayout2);

This doesn't work, I am getting a null pointer exception :

12-16 16:48:52.402 21571-21571/io.apptizer.business.clover E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.apptizer.business.clover, PID: 21571
    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.apptizer.business.clover/io.apptizer.pos.activity.ApplicationSettingsActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at android.view.ViewGroup.addView(ViewGroup.java:3353)
        at android.view.ViewGroup.addView(ViewGroup.java:3336)
        at io.apptizer.pos.activity.applicationSetting.ApplicationSettingsActivityCommon.onCreate(ApplicationSettingsActivityCommon.java:151)
        at io.apptizer.pos.activity.ApplicationSettingsActivity.onCreate(ApplicationSettingsActivity.java:78)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

What can I try to resolve this?

5
  • 1. Where do you call the method? Are the layouts in an activity or fragment? Are you trying to "add" in an activity or fragment? Commented Dec 16, 2020 at 11:33
  • 2
    2. You can also use the include tag, and then change its visibility. Commented Dec 16, 2020 at 11:34
  • 3. Your LinearLayouts are separate xml files so you can't add them as views. Commented Dec 16, 2020 at 11:36
  • Why don't you create the separate xml into a dynamic layout ? Just use the same views from separate xml then add to your main layout. Commented Dec 16, 2020 at 11:37
  • Further explanation of your NPE: you do not have a view with id "storeLocation" inside the activity/fragment you are working in, thus the NPE. You either need to add it as a Fragment or in an Include tag. Commented Dec 16, 2020 at 11:42

1 Answer 1

1

One of the layouts must not exist as a view yet. If your activity or fragment is being built with settingButtonHolder layout then you need to inflate the other layout using a LayoutInflater.

Once you've used the LayoutInflater you will receive a view instance which you can use to add to your linearlayout in the place of linearLayout2. Until you've inflated your new layout, you will keep seeing NPEs because the views in that layout will not exist. You will have to use your inflated view instance (linearLayout2) to access the views, preferably through linearLayout2.findViewById(R.id.X).

Try this out and let me know if it has helped you at all, Panos.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.