指定的子节点已经有父节点。您必须调用removeView()

时间:2012-12-05 19:42:56

标签: android-layout android-view

出于某种原因,我一直收到以下错误:

  

java.lang.IllegalStateException:指定的子级已有父级。您必须在子视图的父视图上调用removeView()

我使用以下代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listitem);
    //url is fetched from another class
    readWebpage(imdbUrl);
}

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

    }

    @Override
    protected void onPostExecute(String result) {
        //textView.setText(result);
        displayMoviesList(result);
    }
}

public void readWebpage(String imdbUrl) {
}

public void displayMoviesList(String result) {
    JSONObject responseObj = null; 
    try {
        responseObj = new JSONObject(result);
        JSONObject Obj = responseObj.getJSONObject("results");
        JSONArray moviesListObj = Obj.getJSONArray("result");

        for(int i=0 ;i<moviesListObj.length();i++) {
            JSONObject e = moviesListObj.getJSONObject(i);
            cover[i] = e.getString("cover");
            title[i] = e.getString("title");
            year[i] = e.getString("year");
            director[i] = e.getString("director");
            rating[i] = e.getString("rating");
            details[i] = e.getString("details");
        }

        tl = (TableLayout) findViewById(R.id.main_table);
        imgView = (ImageView)findViewById(R.id.imageID);
        progressbar = (ProgressBar) findViewById(R.id.loadingBar);
        //new loadImageTask().execute(cover[i].toString());
        new loadImageTask().execute( URL);// it calls another function..
        TextView title = (TextView)findViewById(R.id.titleID);
        title.setText("TEXT");
        TableRow tr = new TableRow(this);
        tr.addView(imgView);
        tr.addView(title);
        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,     LayoutParams.WRAP_CONTENT));
    } catch(JSONException e) {
        //Log.e("log_tag", "Error parsing data " + e.toString());
    }

我不确定为什么我收到错误,但是在我将其添加到视图时似乎正在生成。

1 个答案:

答案 0 :(得分:0)

解决方案是我们要么在.xml文件中定义行,要么在.java文件中定义行。我在我的java文件中动态添加行并使用addView。所以在.xml文件中我们无法添加另一个视图(我们不应该添加相同的行)。