2011-08-09 20 views
0

我有一个ViewFlipper,我想在其中添加视图(相对于儿童的布局)。我正在尝试在AsyncTask中执行此操作。异常添加在ViewFlipper中查看

这里是我使用的代码:

class LoadData extends AsyncTask<Object, Void, String> 
{ 
    RelativeLayout rl_main; 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute();   
    } 

    @Override 
    protected String doInBackground(Object... parametros) 
    { 
     Cursor cur_channel = db_Helper.sqlDB.query(DatabaseHelper.Channels_TableName, null, null, null, null, null, null); 
     startManagingCursor(cur_channel); 

     int index = 0; 
     while(cur_channel.moveToNext()) 
     { 

      LayoutInflater inflater = getLayoutInflater(); 
      rl_main = (RelativeLayout) inflater.inflate(R.layout.newslisting,null); 

      ListView lv_Listing = (ListView) rl_main.findViewById(R.id.id_lv_news_listing); 
      LazyAdapter newsAdpater = new LazyAdapter(NewsListing.this, channel_id, db_Helper); 
      lv_Listing.setAdapter(newsAdpater); 
      lv_Listing.setDividerHeight(0); 

      TextView tv_channelNumber = (TextView)rl_main.findViewById(R.id.id_tv_ChannelNumber); 
      if(tv_channelNumber != null) 
      { 
       tv_channelNumber.setText("Some Text"); 
      } 

      TextView tv_channelName = (TextView)rl_main.findViewById(R.id.id_tv_ChannelName); 
      if(tv_channelName != null) 
      { 
       tv_channelName.setText("Some Text"); 
      } 

      lv_Listing.setOnItemClickListener(new OnItemClickListener() 
      { 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
       { 
        // something on ListItem Click 
       } 
      });   

      publishProgress(); 
      index++; 
     }  
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... v) 
    { 

     super.onProgressUpdate(v); 
     try 
     { 
      viewFlipper.addView(rl_main); // Here I get the exception, but not on all the views 
     } 
     catch (Exception e) 
     { 
      // Exception 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) 
    { 
     super.onPostExecute(result); 
     progressDialog.dismiss(); 
    } 
} 

在保护无效onProgressUpdate(无效... V),我得到的异常,当我添加视图ViewFlipper,但我没有看到这个每次添加View都有例外。例外是:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. in viewflipper 

回答

0

与主题更换的AsyncTask和处理程序所做的工作。现在它的工作很完美。我不知道AsyncTask有什么问题。

1

您应该可能使用重载版本的LayoutInflater.inflate。 在您的代码:

LayoutInflater inflater = getLayoutInflater(); 
rl_main = (RelativeLayout) inflater.inflate(R.layout.newslisting,null); 

使用此方法来代替:Android Developpers LayoutInflater.inflate

布尔attachToRoot是用于连接所述膨胀以他的根(父)或没有。 试图通过假布尔这样的:

rl_main = (RelativeLayout) inflater.inflate(R.layout.newslisting,null, false);