当我在baseadapter的getView方法中单击一个按钮时,我无法在asynctask类的preexecute方法中显示进度对话框。当 pDialog.show()方法ID被调用时,我得到“无法添加窗口异常”异常。当我删除preexecute方法它工作正常。有人可以解释为什么会这样。
这是我的代码:
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait..");
pDialog.setCancelable(false);
pDialog.show();
}
这是我的按钮点击事件代码:
holder.buttonid.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
new MyAsyncTask().execute();
} catch (Exception e)
{
e.printStackTrace();
}
}
});
这是我的BadeAdapter类
公共类FriendListAdapter扩展了BaseAdapter {
private Context context;
private LayoutInflater inflater=null;
private ArrayList<MyObject> mDisplayedValues;
static class ViewHolder
{
ImageView img_friend;
TextView tvname_friend;
Button btnstatus_friend;
}
public FriendListAdapter(Context context, ArrayList<MyObject> arrayList) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.mDisplayedValues = arrayList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mDisplayedValues.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view =convertView;
ViewHolder holder = null;
if(convertView==null)
{
view = inflater.inflate(R.layout.inflate_view,parent,false);
holder = new ViewHolder();
holder.buttonid= (Button)view.findViewById(R.id.btnid);
view.setTag(holder);
}
else
{
holder = (ViewHolder)view.getTag();
}
try
{
holder.buttonid.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
new MyAsyncTask().execute();
} catch (Exception e)
{
e.printStackTrace();
}
}
});
} catch (Exception e)
{
e.printStackTrace();
}
return view;
}
private class MyAsyncTask extends AsyncTask<String, Void, Void> {
private ProgressDialog pDialog = null;
private boolean hasExceptionOccured = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
//Intent to next activity
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}