1

I have a LinearLayout with Vertical orientation and I would like to add another horizontal LinearLayout with two childs.

Here is my xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Gray_background"
android:orientation="vertical" >

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="10dp"
    android:background="@color/GrayStroke" />

<LinearLayout
    android:id="@+id/contacts_tab_menulist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:orientation="vertical" >
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/GrayStroke" /> </LinearLayout>

Here is the code which should do the job:

View rootView = inflater.inflate(R.layout.contacts_tab,
            container, false);      

        contacts_tab_menulist = (LinearLayout) getActivity().findViewById(R.id.contacts_tab_menulist);
    //      add LayoutParams
    LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 35);
    LinearLayout listItem = new LinearLayout(getActivity());
    listItem.setLayoutParams(lparams);
    listItem.setOrientation(LinearLayout.HORIZONTAL);

    AspectRatioImageView icon = new AspectRatioImageView(getActivity());
    LinearLayout.LayoutParams ic_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 25);
    icon.setImageResource(R.drawable.contacts_add_contact);
    icon.setLayoutParams(ic_params);
    listItem.addView(icon);

    TextView title = new TextView(getActivity());
    LinearLayout.LayoutParams t_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    title.setText("Add Contact");
    title.setId(0);
    title.setLayoutParams(t_params);
    listItem.addView(title);

    contacts_tab_menulist.addView(listItem);
return rootView;

I get a nullPointerException on contacts_tab_menulist.addView(listItem). What am I doing wrong?

5
  • You probably need to setContentView(R.layout.<xml_file_name>);, or getLayoutInflater().inflate(R.layout.<xml_file_name>, null);. Commented Sep 8, 2014 at 19:44
  • This should help you: <stackoverflow.com/questions/6661261/…> Commented Sep 8, 2014 at 19:46
  • @myninjaname I updated the question, please take a look. Commented Sep 8, 2014 at 19:48
  • @David in the answer, I am doing exactly the same thing. But I still get same error. Commented Sep 8, 2014 at 19:50
  • contact_tab_menulist is null, is contacts_tab_menulist contained in your activity layout? Commented Sep 8, 2014 at 19:51

2 Answers 2

2

Try replacing

contacts_tab_menulist = (LinearLayout) getActivity().findViewById(R.id.contacts_tab_menulist);

with

contacts_tab_menulist = (LinearLayout) rootView.findViewById(R.id.contacts_tab_menulist);
Sign up to request clarification or add additional context in comments.

Comments

1

Get the view from your inflated view, since inflating the View does not mean its the current activities view:

    contacts_tab_menulist = (LinearLayout) rootView.findViewById(R.id.contacts_tab_menulist);

1 Comment

Thank you. I tried this earlier but it did not help, I guess I had another error. It works now.

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.