title: Can't dynamically add views "child has a parent" android
body: <p>Hi guys I'm trying to make this app generate many views based on existing database and xml files.My app is crushing and Im  getting error like <code>"java.lang.RuntimeException: Unable to start activity ComponentInfo{hovi.akapbox/hovi.akapbox.CytatyActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."</code></p>
  
  <p>Here is code:
  -CytatyActivity.java:</p>
  
  <pre><code>public class CytatyActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_cytaty);
      SqlLiteDataBaseHelper sqlLiteDataBaseHelper = new SqlLiteDataBaseHelper(this);
  
      LinearLayout ll = (LinearLayout) findViewById(R.id.linear_cytaty);
      try {
  
          if(sqlLiteDataBaseHelper.checkDataBase()){
  
              Log.e("db: ","Data Base Already Exists");
  
          }else {
  
              sqlLiteDataBaseHelper.CopyDataBaseFromAsset();
  
          }
  
      }catch (Exception e){
          e.printStackTrace();
      }
      try {
  
          sqlLiteDataBaseHelper.openDataBase();
          // after open data  base u can read write data base
  
      }catch (Exception e){
          e.printStackTrace();
      }
      LayoutInflater factory = getLayoutInflater();
      RelativeLayout relativeLayout = new RelativeLayout(this);
      View v = factory.inflate(R.layout.layout_cytaty_text, relativeLayout,false);
      View v2 = factory.inflate(R.layout.layout_cytaty_image, relativeLayout ,false);
  
      Cursor cursor = sqlLiteDataBaseHelper.db.rawQuery("SELECT * FROM Cytaty",null);
      Context cont = getApplicationContext();
  
      if( cursor.moveToFirst()) {
  
  
          do {
  
              // fill in any details dynamically here
              TextView textView = (TextView) v.findViewById(R.id.TV_cytat);
              textView.setText(cursor.getString(cursor.getColumnIndex("text")).toString());
  
  
              ImageView imageView = (ImageView) v2.findViewById(R.id.IV_cytat);
              Log.v("XD","XDDD");
              int id = (int)cont.getResources().getIdentifier(cursor.getString(cursor.getColumnIndex("imagescr")).toString(), "drawable", cont.getPackageName());
              imageView.setImageResource(id);
  
  
              relativeLayout.addView(textView);
              relativeLayout.addView(imageView);
  
              ll.addView(relativeLayout, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  
          } while (cursor.moveToNext());
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>-layout_cytaty_image.xml:</p>
  
  <p>Any ideas what to do?
  I've tryied to add sth like: </p>
  
  <pre><code>((ViewGroup)imageView.getParent()).removeView(imageView);
  </code></pre>
  
  <p>but it didnt work. I got error that im using remoteView() on blank argument.</p>