0

The code for the adding the view at run time is as follows: contactActivity.java:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_contact_details);

        mPhoneList = (LinearLayout) findViewById(R.id.contact_phone_list);

        View view = getLayoutInflater().inflate(R.layout.contact_details_item, mPhoneList,false);
        mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
        mItemValue = (TextView) view.findViewById(R.id.contact_item_value);



        //mContactItem = (LinearLayout) findViewById(R.id.contact_item);

        mItemLabel.setText("home");  //NULL POINTER EXCEPTION HERE
        mItemValue.setText("999999999999");

        mPhoneList.addView(view);

        mItemLabel.setText("work");
        mItemValue.setText("999gggggggggg");
        mPhoneList.addView(view);   


    }

contact_phone_list is in activity_contact_details.xml:

some code...
<LinearLayout
                android:id="@+id/contact_phone_list"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:background="@color/silver"
                android:orientation="horizontal"
                android:padding="16dp" >

            </LinearLayout>
some more code...

contact_details_item.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contact_item"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView 
        android:id="@+id/contact_item_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:textColor="@color/orange"
        android:gravity="right"/>

    <TextView 
        android:id="@+id/contact_item_value"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:textStyle="bold"
        android:gravity="left"/>

</LinearLayout>

logcat:

09-18 19:18:22.359: E/AndroidRuntime(28218):    at dalvik.system.NativeStart.main(Native Method)
09-18 19:18:22.359: E/AndroidRuntime(28218): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addView(ViewGroup.java:3249)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addView(ViewGroup.java:3194)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addView(ViewGroup.java:3170)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at com.glad2.ui.ContactDetailsActivity.onCreate(ContactDetailsActivity.java:51)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.app.Activity.performCreate(Activity.java:5008)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-18 19:18:22.359: E/AndroidRuntime(28218):    ... 11 more 

Could someone please help me understand why the crash is coming?

MODIFIED CODE (Now i am getting a blank screen with no data):

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_contact_details);

        mPhoneList = (LinearLayout) findViewById(R.id.contact_phone_list);
        mEmailList = (LinearLayout) findViewById(R.id.contact_email_list);

        for (int i=0; i<5;i++){
            View view = getLayoutInflater().inflate(R.layout.contact_details_item, mPhoneList,false);
            mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
            mItemValue = (TextView) view.findViewById(R.id.contact_item_value);


            //mContactItem = (LinearLayout) findViewById(R.id.contact_item);
            String t[] = new String[] {"abc", "def", "df", "sd", "kg", "lk"}; 
            mItemLabel.setText(t[i]);
            mItemValue.setText(String.valueOf(i));

            mPhoneList.addView(view);

        }

Thanks and regards, Sunny

Sunny
  • 7,444
  • 22
  • 63
  • 104

3 Answers3

1

You need to change these lines

mItemLabel = (TextView) findViewById(R.id.contact_item_label);
mItemValue = (TextView) findViewById(R.id.contact_item_value);

to

mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
mItemValue = (TextView) view.findViewById(R.id.contact_item_value);

The way it is currently written, you are looking in the activity_contact_details.xml for these TextViews but you need to be looking in the contact_details_item.xml for the ids since that's where they live.

If this doesn't solve your problem then please post the logcat.

Edit

Have "home" and "work" LinearLayouts in your xml wrapped in a root LinearLayout then these will both be in your view and only add view to mPhoneList once. You can later add LinearLayouts dynamically if you will have an undetermined amount of "items" (eg. mobile, work2, etc...)

Ex.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
   <LinearLayout
       android:id="@+id/homeLL
       // add other properties, use default horizontal orientation>
      <TextView 
          android:id="@+id/contact_item_label"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.3"
          android:textColor="@color/orange"
          android:gravity="right"/>

      <TextView 
          android:id="@+id/contact_item_value"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.7"
          android:textStyle="bold"
          android:gravity="left"/>
     </LinearLayout
     <LinearLayout
         android:id="@+id/workLL
        // add other properties>
       //add your TextViews as in your first LL
     </LinearLayout>
</LinearLayout>

Dynamically create Views

Create LinearLayout Dynamically

Add TextViews to LinearLayout

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • thanks for the explanation also. now i am getting the following exception. i have updated the main question with the logcat – Sunny Sep 18 '13 at 13:50
  • 1
    You are calling `mPhoneList.addView(view);` twice so after the first time it already has a parent. A `View` may have only one direct parent. Remove one of these calls – codeMagic Sep 18 '13 at 13:53
  • but i want to add a list of items. phone numbers and labels. so how would i do that then – Sunny Sep 18 '13 at 13:55
  • Create different `TextView`s for each and add them to `View` or whatever `layout`. Or multiple `LinearLayout`s but you can't reuse the same one like that. Then just add `View` once. – codeMagic Sep 18 '13 at 13:58
  • See my edit. I hope that helps...I gave more than I was going to but feel free to ask if you don't understand. – codeMagic Sep 18 '13 at 15:17
  • Thanks for the explanation. but i do have a liner layout (contact_details_item). So i was hoping to add each item using this linearlayout. how would that work? i mean how would i dynamically add those multiple linearlayouts. thanks. i actually dont want to hardcode anything in the xml. show it based on wht i receive as input – Sunny Sep 18 '13 at 15:20
  • So, depending on your criteria you would create a new `LinearLayout` with however many `TextView`s you need then add each of those to a root `LinearLayout` like `view` and add that to `mPhoneList` as you are doing. – codeMagic Sep 18 '13 at 15:36
  • I have added a couple links to dynamically create `View`s. I hope that helps. This should get you started and I don't want to write all of the code. But give that a try and there are plenty of examples on SO and The Google if you get stuck. – codeMagic Sep 18 '13 at 15:39
  • thanks a lot. i tried out something and i am getting a blank screen. could you please have a look? – Sunny Sep 18 '13 at 15:45
  • @Sunny I don't know if this is your problem but `LinearLayout height` in `contact_details_item.xml` should probably be `wrap_content` so you have room for the others – codeMagic Sep 18 '13 at 17:30
0

Try this:

View view = getLayoutInflater().inflate(R.layout.contact_details_item, mPhoneList,false);
    mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
    mItemValue = (TextView) view.findViewById(R.id.contact_item_value);

You should search for contact_item_label and contact_item_value in your inflated view. Not in the content layout of activity.

amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

You should put :

   mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
    mItemValue = (TextView) view.findViewById(R.id.contact_item_value);

instead of :

 mItemLabel = (TextView) findViewById(R.id.contact_item_label);
    mItemValue = (TextView) findViewById(R.id.contact_item_value);

and it should work correctly

Houssem
  • 36
  • 2