Question

I am new in android.Now i am doing in AsyncTask in ArrayAdapter.When I use the ProgressDialog it shows following error,

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
        04-25 08:07:40.881: E/AndroidRuntime(6417):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:563)
        04-25 08:07:40.881: E/AndroidRuntime(6417):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)

Following is my code,

public class ExpandableListAdapter1 extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;


    CommonFunctions cfObj;
    public ExpandableListAdapter1(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_seconditem, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        Button mtAccept=(Button)convertView.findViewById(R.id.btnAccept);
        Button mtReject=(Button)convertView.findViewById(R.id.btnReject);

        mtAccept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("Accept clickable");
                System.out.println("Group number"+groupPosition);
                System.out.println("child number"+childPosition);

                //_context.startService(new Intent(_context, GroupResponds.class));
                new Worker().execute();

            }
        });

        mtReject.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("Reject clickable");
            }
        });

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        ImageView img_selection=(ImageView) convertView.findViewById(R.id.imageside);
        int imageResourceId = isExpanded ? R.drawable.btn_up
                : R.drawable.btn_down;
        img_selection.setImageResource(imageResourceId);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class Worker extends AsyncTask<Integer, String, Integer>
    {
        ProgressDialog dialog;
        boolean device_error=false;
        String temp_encrypted_device_id=null;
        String result=null;
        String device_id="";


        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            cfObj=new CommonFunctions(_context);
            dialog=new ProgressDialog(_context);

            System.out.println("first....");

            dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
                public void onCancel(DialogInterface dialog) 
                {
                    Worker.this.cancel(true);
                }
            });
            System.out.println("second....");
            dialog.setMessage(_context.getResources().getString(R.string.initilalising));
            System.out.println("third....");
            if(dialog==null){
                System.out.println("dialog null....");
            }else{
            dialog.show();
            }
            System.out.println("fourth....");

        }
        @Override
        protected Integer doInBackground(Integer... params) {
            // TODO Auto-generated method stub
            device_id=cfObj.get_device_details();
            if(device_id==null||device_id=="")
            {
                device_error=true;
            }
            else
            {
                device_error=false;
            }
            if(device_error!=true)
            {
                publishProgress(_context.getString(R.string.internet_connectivity_checking));
            }

            return null;
        }
        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            System.out.println("device id"+device_id);
            dialog.dismiss();
        }
        @Override
        protected void onProgressUpdate(String... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            dialog.setMessage(values[0]);
        }


    }   
Was it helpful?

Solution

Seems like your member variable _context is is an invalid Context for creating dialogs. You need to pass your Activity to your adapter, not the application context obtained with getApplicationContext().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top