0

I'm unable to display the progress dialog in preexecute method of asynctask class when a button is clicked in getView method of my baseadapter. When pDialog.show() method id called I'm getting "Unable to add window exception" exception. When I removed preexecute method it is working fine. Can someone explain why this is happening.

here is my code:

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Please wait..");
        pDialog.setCancelable(false);
        pDialog.show();  
    }

This is my button click event code:

 holder.buttonid.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            try 
            {
                new MyAsyncTask().execute();                        
            } catch (Exception e)
            {
                e.printStackTrace();
            }               
        }
    });

This is my BadeAdapter class

public class FriendListAdapter extends 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();


    }


} 



}

1 Answers1

0

You need to use the context of the activity to show a Dialog

pDialog = new ProgressDialog(YourActivity.this);

Here some more information.

Community
  • 1
  • 1
Merlevede
  • 8,140
  • 1
  • 24
  • 39