我有一个异步任务。
protected class InitTask extends AsyncTask<Context, Integer, String> {
View eachLayout;
@Override
protected String doInBackground(Context... params) {
try {
myfunction();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
linearLayout.invalidate();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
linearLayout.addView(eachLayout, params);
linearLayout.invalidate();
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onCancelled()
*/
@Override
protected void onCancelled() {
super.onCancelled();
}
public void redrawLayout(View linearLayout) {
try {
eachLayout = linearLayout;
publishProgress();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void myFunction() {
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
params = new LinearLayout.LayoutParams(
newLinearLayout.getLayoutParams().width,
newLinearLayout.getLayoutParams().height);
for (int i = 0; i < list.size(); i++) {
final View eachLayout = layoutInflater.inflate(R.layout.sample, null);
//..........................
eachLayout.invalidate();
initTask.redrawLayout(eachLayout);
}
}
params = new LinearLayout.LayoutParams(
linearLayout.getLayoutParams().width,
linearLayout.getLayoutParams().height);
它在这一行显示IllegalStateException linearLayout.addView(eachLayout, params); .(这个孩子已经有一个父母)。
我试过 linearLayout.removeAllViews() 等......但它不起作用。
如何解决这个问题?
提前致谢