ViewFlipper.addView() 在 AsyncTask 中显示进度对话框时出现异常

发布于 2024-11-25 05:23:40 字数 2153 浏览 0 评论 0原文

我想要什么:我想在动态添加视图到 ViewFlipper 时显示一个进度对话框。

我拥有的:我使用 AsyncTask 来实现此目的。 ViewFlipper 在我的主 Activity 中声明,我在 AsyncTask 的 doInBackground() 中向 ViewFlipper 添加视图。

问题是什么: 我在 viewFlipper.addView() 语句上遇到异常,异常是“ Main hasleaked window com.android.internal.policy.impl ..... 那是最初在这里添加了“,类似这样的内容。

这是代码:

public class Main extends Activity
{   

   private ViewFlipper viewFlipper;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_flipper);

      viewFlipper = (ViewFlipper)findViewById(R.id.id_vf_main);
      new LoadData().execute();
      // Some other tasks.
   }


   class LoadData extends AsyncTask<Object, Void, String>
   {
      protected ProgressDialog  progressDialog;

      @Override
      protected void onPreExecute()
      {
          super.onPreExecute();         
          progressDialog = ProgressDialog.show(Main.this,"Loading", "Loading Data...", true, false);
      }

      @Override
      protected String doInBackground(Object... parametros)
      {
          for (int i = 0; i < Login.data_Channel_Name.size(); i++)
          {
            LayoutInflater inflater = getLayoutInflater();
            RelativeLayout rl_main = (RelativeLayout) inflater.inflate(R.layout.main,null); 

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

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

            viewFlipper.addView(rl_main);
          }     
          return null;
      }

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

What I want: I want to show a progress dialog while adding views dynamically to my ViewFlipper.

What I have: I have used an AsyncTask to achieve this. The ViewFlipper is declared in my Main Activity, I am adding views to ViewFlipper in the doInBackground() of AsyncTask.

What is the problem: I am getting an exception on the viewFlipper.addView() statement and the exception is " Main has leaked window com.android.internal.policy.impl ..... that was originally added here. " , something like this.

Here is the code:

public class Main extends Activity
{   

   private ViewFlipper viewFlipper;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_flipper);

      viewFlipper = (ViewFlipper)findViewById(R.id.id_vf_main);
      new LoadData().execute();
      // Some other tasks.
   }


   class LoadData extends AsyncTask<Object, Void, String>
   {
      protected ProgressDialog  progressDialog;

      @Override
      protected void onPreExecute()
      {
          super.onPreExecute();         
          progressDialog = ProgressDialog.show(Main.this,"Loading", "Loading Data...", true, false);
      }

      @Override
      protected String doInBackground(Object... parametros)
      {
          for (int i = 0; i < Login.data_Channel_Name.size(); i++)
          {
            LayoutInflater inflater = getLayoutInflater();
            RelativeLayout rl_main = (RelativeLayout) inflater.inflate(R.layout.main,null); 

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

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

            viewFlipper.addView(rl_main);
          }     
          return null;
      }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨死的猪 2024-12-02 05:23:40

您正在尝试从后台线程与 UI 进行交互,这是不允许的。您可以在 onPostExecute()onPreExecute() 方法中扰乱 UI。还有另一种方法; onProgressUpdate() 您可以像这样使用:

protected void onProgressUpdate(Integer... progress) {
// on UI thread!
    viewFlipper.addView(rl_main);
}

显然您需要通过创建字段或其他内容来处理将视图传递给此方法。您可以在 doInBackground() 中使用以下命令调用此函数:

publishProgress(0);

该函数通常用于更新加载栏或类似内容上的百分比(因此传递整数),但应该可以很好地满足您的目的。

You are trying to interact with the UI from the background thread which isn't allowed. You are allowed to mess with the UI in your onPostExecute() and onPreExecute() methods. There is another method; onProgressUpdate() you can use like so:

protected void onProgressUpdate(Integer... progress) {
// on UI thread!
    viewFlipper.addView(rl_main);
}

Obviously you'll need to handle passing the view to this method by creating a field or something. You can call this within your doInBackground() with the following:

publishProgress(0);

The function is typically used for updating the percentage on a loading bar or similar (hence passing the integer) but should work fine your your purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文