在ViewFlipper中添加View的异常

时间:2011-08-09 07:51:27

标签: android android-layout android-asynctask

我有一个ViewFlipper,我想在其中添加视图(带子项的相对布局)。我试图在AsyncTask中执行此操作。

以下是我正在使用的代码:

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();
    }
}

在protected void onProgressUpdate(Void ... 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

2 个答案:

答案 0 :(得分: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); 

答案 1 :(得分:0)

用Thread和Handler替换AsyncTask完成了工作。它的工作完美了。我不知道AsyncTask有什么问题。