ViewFlipper.addView() 在 AsyncTask 中显示进度对话框时出现异常
我想要什么:我想在动态添加视图到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试从后台线程与 UI 进行交互,这是不允许的。您可以在 onPostExecute() 和 onPreExecute() 方法中扰乱 UI。还有另一种方法; onProgressUpdate() 您可以像这样使用:
显然您需要通过创建字段或其他内容来处理将视图传递给此方法。您可以在 doInBackground() 中使用以下命令调用此函数:
该函数通常用于更新加载栏或类似内容上的百分比(因此传递整数),但应该可以很好地满足您的目的。
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:
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:
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.