进度对话框Android

时间:2014-04-25 12:12:49

标签: android progressdialog

我是android的新手。现在我正在AsyncTask ArrayAdapter进行。当我使用ProgressDialog时,它显示以下错误,

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)

以下是我的代码,

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]);
        }


    }   

1 个答案:

答案 0 :(得分:0)

似乎您的成员变量_context是无效的Context,用于创建对话框。您需要将Activity传递给适配器,而不是通过getApplicationContext()获取的应用程序上下文。