XML : How to display views from an array in a layout and avoid "specified child already has a parent"

I'm trying to make an activity that puts messages in views and displays them in order when clicked on the layout. But when i click on the layout 2 times, it works perfectly. 3rd time the app crashes giving me this error :- "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."

I've tried everything possible that could be done with inflater parameter i.e. null, true and false. Nothing works. I need to complete this app by tomorrow. Any help is appreciated.

public class MainActivity extends ActionBarActivity {

  private ViewGroup mChatLayout;  private TextView mNatashaMessage;  private TextView mVanditMessage;  private View[] v;  private View[] n;  private int mV;  private int mN;  private int[] mOrder0 = {0,1,1,0,1,0,1,1,0,1};  private String[] mNatasha0 = {"Message example 1", "Message example 2", "Message example 3", "Message example 4"};  private String[] mVandit0 = {"Message from me 1", "Message from me 2", "Message from me 3", "Message from me 4", "Message from me 5", "Message from me 6"};  private int loop = 0;        @Override  protected void onCreate(Bundle savedInstanceState)  {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);            mChatLayout = (ViewGroup) findViewById(R.id.chatParent);          addViews();          mChatLayout.setOnClickListener(new View.OnClickListener()      {          @Override          public void onClick(View v)          {                  showChat(loop);            }      });          }    private void addViews()  {      LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);      v = new View[mVandit0.length];      n = new View[mNatasha0.length];            for(int i = 0; i<mNatasha0.length; i++)      {          View viewN;          viewN = inflater.inflate(R.layout.layout_natasha, mChatLayout, false);          mNatashaMessage = (TextView) viewN.findViewById(R.id.messageNatasha);            mNatashaMessage.setText(mNatasha0[i]);          n[i] = viewN;          }      for(int i = 0; i<mVandit0.length; i++)      {          View viewV;          viewV = inflater.inflate(R.layout.layout_vandit, mChatLayout, false);          mVanditMessage = (TextView) viewV.findViewById(R.id.messageVandit);            mVanditMessage.setText(mVandit0[i]);          v[i] = viewV;        }  }        private void showMessageVandit(int n)  {        LinearLayout vanditMsg = (LinearLayout) this.v[n];      mChatLayout.addView(vanditMsg);  }    private void showMessageNatasha(int v)  {        LinearLayout natashaMsg = (LinearLayout) this.n[v];      mChatLayout.addView(natashaMsg);  }    private void showChat(int x)  {        mV = 0;      mN = 0;                        switch(mOrder0[x])              {                    case 1:                      if(mV==v0.length)                          break;                  showMessageVandit(mV);                    mV++;                      break;                        case 0:                        if(mN==n0.length)                          break;                  showMessageNatasha(mN);                    mN++;                      break;                    default:                      break;                  }            loop++;  }}    

activity_main.xml

  <RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:paddingLeft="@dimen/activity_horizontal_margin"      android:paddingRight="@dimen/activity_horizontal_margin"      android:paddingTop="@dimen/activity_vertical_margin"      android:paddingBottom="@dimen/activity_vertical_margin"      android:orientation="vertical"      tools:context="com.blogspot.ticklinggenre.happybirthdaybaby.ui.ConversationActivity"      android:background="#ffbababa">        <LinearLayout          android:layout_width="match_parent"          android:layout_height="match_parent"          android:paddingLeft="0dp"          android:paddingRight="0dp"          android:paddingTop="0dp"          android:paddingBottom="0dp"          android:orientation="vertical"          tools:context="com.blogspot.ticklinggenre.happybirthdaybaby.ui.ConversationActivity"          android:background="#ffbababa"          android:id="@+id/chatLayout">            <ImageView              android:layout_width="fill_parent"              android:layout_height="wrap_content"              android:id="@+id/monthImage"/>          <TextView              android:layout_width="match_parent"              android:layout_height="40dp"              android:text="Chat With Vandit Narain Tyagi"              android:textAlignment="center"              android:textStyle="bold"              android:textSize="23sp"              android:background="#ff4963a4"              android:autoText="false"              android:textColor="#ffffffff"/>          <ScrollView              android:layout_width="fill_parent"              android:layout_height="400dp">              <RelativeLayout                  android:layout_width="fill_parent"                  android:layout_height="400dp"                  android:id="@+id/chatParent"                  android:orientation="vertical">                  <LinearLayout                      android:layout_width="wrap_content"                      android:id="@+id/mChatVandit"                      android:layout_height="wrap_content"                      android:layout_alignParentRight="true"                      android:orientation="horizontal">                    </LinearLayout>                  <LinearLayout                      android:layout_width="wrap_content"                      android:layout_height="wrap_content"                      android:id="@+id/mChatNatasha"                      android:layout_alignParentLeft="true"                      android:orientation="horizontal">                    </LinearLayout>                      </RelativeLayout>          </ScrollView>          </LinearLayout>        <Button          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:id="@+id/firstConversationButton"          android:layout_above="@+id/secondConversationButton"/>      <Button          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:id="@+id/secondConversationButton"          android:layout_alignParentBottom="true"/>  </RelativeLayout>    

layout_natasha.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                    android:layout_width="fill_parent"                  android:layout_height="wrap_content"                  android:layout_alignParentLeft="true"                  android:paddingTop="10dp"                  android:paddingBottom="10dp"                  android:id="@+id/Natasha">                  <ImageView                      android:layout_width="50dp"                      android:layout_height="50dp"                      android:src="@mipmap/contact_natasha"                      />                  <TextView                      android:layout_width="wrap_content"                      android:layout_height="50dp"                      android:paddingLeft="25dp"                      android:id="@+id/messageNatasha"                      android:textColor="#ff000000"                      android:textStyle="bold|italic"                      android:textAlignment="textStart"                      android:textSize="15sp"/>          </LinearLayout>    

layout_vandit.xml

  <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentRight="true"                android:layout_below="@+id/Natasha"                android:paddingTop="10dp"                android:paddingBottom="10dp"                android:id="@+id/Vandit">      <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/messageVandit"          android:textColor="#ff000000"          android:textStyle="bold|italic"          android:textAlignment="textEnd"          android:textSize="20sp"          android:paddingRight="10dp"/>      <ImageView          android:layout_width="50dp"          android:layout_height="50dp"          android:src="@mipmap/contact_vandit"/>    </LinearLayout>    

No comments:

Post a Comment