异步任务错误Android

时间:2013-12-17 10:31:37

标签: android

我正在开发一个连接到Web服务并在列表视图中显示数据的应用程序。我可以成功显示数据[我使用了自定义列表视图并显示我使用自定义适配器类的数据由基础适配器扩展]。现在我正在尝试连接到另一个Web服务时,单击一个接受按钮[在列表视图中]。我已经开发了从服务中获取字符串响应的方法,并且这些方法运行良好。[我已经通过我的应用程序进行了测试]。

我的问题是,我希望在连接到服务时显示一些进度对话框,并使用Toast消息显示响应。我已经使用AsyncTask类了。首先,我刚刚运行一个线程让它睡了一段时间。这与我的AsyncTask完全一致。但是当我在[{1}}]内使用它来调用Web服务时,我收到了一个错误:

  

活动com.jsonlist.jsonlist.ViewList已泄露窗口   com.android.internal.policy.impl.PhoneWindow$DecorView@41814e38那个   最初在这里添加了android.view.WindowLeaked:Activity   com.jsonlist.jsonlist.ViewList已泄露窗口   com.android.internal.policy.impl.PhoneWindow$DecorView@41814e38那个   最初是在这里添加的   android.view.ViewRootImpl。(ViewRootImpl.java:374)at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)

我的代码如下:

此方法位于我的Adapter类

doInBackground()

当我按下对话框中的“是”按钮时。它应该调用我的 public void dialogshow(final String Date,final String Start,final String End){ AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity); alertDialogBuilder.setTitle("Confirm your Action!"); // set dialog message alertDialogBuilder .setMessage("Click yes Confirm!!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { //Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show(); new ShowResponceForAccept().execute(); } }) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); alertDialogBuilder.show(); }

这是我的AsyncTask

AsyncTask

这也在同一个适配器类中。

有人可以帮我吗??

修改

这是我的完整适配器类

    public class ShowResponceForAccept extends AsyncTask<String, String, String>{

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();


        pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);

    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
    //  ShortList sendandget = new ShortList();
    //  String resp = sendandget.getResponceFromServer(Appid, null, Uid);

        ShortList sendandget = new ShortList();
        String resp = sendandget.getResponceFromServer(Appid,"",Uid);
        //Thread.sleep(10000);
        //String x="Your Request Accepted";

        return resp;
        //return x;
    }

    @Override
    protected void onPostExecute(String resp) {
        // TODO Auto-generated method stub
         pDialog.dismiss();


         String x = resp;



         Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();


    }


}

/ * if(resp.equalsIgnoreCase(“ - 1”)){                  Toast.makeText(mContext,“Accepted”,Toast.LENGTH_LONG).show();

public class NewsRowAdapter extends BaseAdapter  {

static Dialog dialogs;
private static final String STIME = "StartTime";
private static final String END = "EndTime";
private static final String DATE = "Date";
private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
public ImageLoader imageLoader; 

String Uid = MainActivity.confirm;
String getDate;
String getStartTime;
String getEndTime;
String Appid;
    //String response;
    //Context context;
    //Initialize adapter
    public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d) {
        super();
        this.resource=resource;
        this.data = d;
        this.activity = act;
        this.mContext = ctx;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());


    }

    public void showFirstDialog(final ArrayList<HashMap<String, String>> list){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Confirm your Action!");

        // set dialog message
        alertDialogBuilder
            .setMessage("You Have Similar Kind of Appoinments!! Do you wanna Show them ?")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {

                    //Toast.makeText(mContext, "Showing", Toast.LENGTH_LONG).show();
                    dialogpop(list);


                }
              })
              .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });
        alertDialogBuilder.show();

    }

    public void dialogshow(final String Date,final String Start,final String End){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Confirm your Action!");

        // set dialog message
        alertDialogBuilder
            .setMessage("Click yes Confirm!!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {




                    //Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();



                    new ShowResponceForAccept().execute();
                    dialog.dismiss();


                }
              })
              .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });
        alertDialogBuilder.show();

    }

    public void dialogshowForReject(){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Confirm your Action!");

        // set dialog message
        alertDialogBuilder
            .setMessage("Click yes Confirm!!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {


                    //Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();

                    new ShowResponceForReject().execute();
                //  loadListAgain();


                }
              })
              .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });
        alertDialogBuilder.show();

    }  



    public void showDuplicateDialog(ArrayList<HashMap<String, String>> list){

        //CharSequence[] cs = list.toArray(new CharSequence[list.size()]);


        AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
        LayoutInflater infl = activity.getLayoutInflater();
        View view = infl.inflate(R.layout.dialog_list, null);

        ListView lv = (ListView) view.findViewById(R.id.dialogList);

        //NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, list);

        SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row,  new String[] { STIME,END, DATE }, new int[] {
                R.id.stime2,R.id.etime2, R.id.blank2});
        lv.setAdapter(sim);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        });



        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.two_line_list_item, android.R.id.text1, Names);*/

        alertDialogBuilder2.setView(view)
        /*alertDialogBuilder2.setAdapter(sim, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        })
        */



        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

        alertDialogBuilder2.show();
    }



@Override
public View getView(final int position, View convertView, final ViewGroup parent) {


    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row,null);


        final TextView firstname = (TextView) vi.findViewById(R.id.fname);
        final TextView lastname = (TextView) vi.findViewById(R.id.lname);
        final TextView startTime = (TextView) vi.findViewById(R.id.stime);
        final TextView endTime = (TextView) vi.findViewById(R.id.etime);
        final TextView date = (TextView) vi.findViewById(R.id.blank);
        final TextView hidID = (TextView) vi.findViewById(R.id.hidenID);
        final TextView hidAppid = (TextView) vi.findViewById(R.id.HidAppoinmentID);
        final ImageView img = (ImageView) vi.findViewById(R.id.list_image);


        HashMap<String, String> song = new HashMap<String, String>();
        song =data.get(position);

        firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
        lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
        startTime.setText(song.get(MainActivity.TAG_STIME));
        endTime.setText(song.get(MainActivity.TAG_ETIME));
        date.setText(song.get(MainActivity.TAG_DATE));
        hidID.setText(song.get(MainActivity.TAG_HID));
        hidAppid.setText(song.get(MainActivity.TAG_HIDApp));

        String theUrl = song.get(MainActivity.TAG_IMG);

        if(theUrl.equalsIgnoreCase("null")){
            Bitmap bImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.propic);
            img.setImageBitmap(bImage);


            //ViewList v = new ViewList();
            //v.handleImage(theUrl, img);
        }
        else{

            /// replace these codes


        Profile pc = new Profile();

        Bitmap map =pc.downloadImage(theUrl);
        img.setImageBitmap(map);







       // imageLoader.DisplayImage(theUrl, img);
        }
        Button accept = (Button) vi.findViewById(R.id.btnAccepted);
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int x = (int) getItemId(position);
                /*Intent zoom=new Intent(mContext, Profile.class);
                zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(zoom);*/

                // get the intent from the hashmap check if there is similar date and time.
                //then store them in a list or array.

                 getDate = (String) date.getText();
                 getStartTime = startTime.getText().toString();
                 getEndTime = endTime.getText().toString();
                 Appid = hidAppid.getText().toString();

                ShortList sh = new ShortList();

                ArrayList<HashMap<String, String>> duplicateList; 
                duplicateList=sh.getDuplicated(getDate, getStartTime, getEndTime);

                if(duplicateList.size()>1){
                    //dialogshow(getDate,getStartTime,getEndTime);
                    showFirstDialog(duplicateList);
                }
                else{
                    dialogshow(getDate, getStartTime, getEndTime);

                }




            }
    });


        Button reject = (Button) vi.findViewById(R.id.btnReject); 
        reject.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                // get User ID  and get request ID
                //then call the method getResponceFromServerForReject()

                dialogshowForReject();
                //MainActivity ma = new MainActivity();
                //
                //ma.new JSONParse().execute();








            }
        });


        vi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String getPname = hidID.getText().toString();

                Toast.makeText(parent.getContext(), "view clicked: "+getPname , Toast.LENGTH_SHORT).show();

                //get the id of the view
                //check the id of the request
                //call the web service acording to the id





                Intent zoom=new Intent(parent.getContext(), Profile.class);  
                zoom.putExtra("PatientID", getPname);
                parent.getContext().startActivity(zoom);



            }
        });

        return vi;


}

public void dialogpop(ArrayList<HashMap<String, String>> list){

    dialogs = new Dialog(activity);
    dialogs.setContentView(R.layout.dialog_list);
    dialogs.setTitle("Select One");



    ListView listView = (ListView) dialogs.findViewById(R.id.dialogList);

    //SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row,  new String[] { STIME,END, DATE }, new int[] {
    //        R.id.stime2,R.id.etime2, R.id.blank2});

    Adapter_For_Dialog nw = new Adapter_For_Dialog(mContext,activity, R.layout.dialog_row, list);
    listView.setAdapter(nw);



    dialogs.show();

}


public class ShowResponceForAccept extends AsyncTask<String, String, String>{

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();


        pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);

    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
    //  ShortList sendandget = new ShortList();
    //  String resp = sendandget.getResponceFromServer(Appid, null, Uid);

        ShortList sendandget = new ShortList();
        String resp = sendandget.getResponceFromServer(Appid,"",Uid);
        //Thread.sleep(10000);
        //String x="Your Request Accepted";

        return resp;
        //return x;
    }

    @Override
    protected void onPostExecute(String resp) {
        // TODO Auto-generated method stub
         pDialog.dismiss();


         String x = resp;



         Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();


    }


}


public class ShowResponceForReject extends AsyncTask<String, Void, String>{

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        /*pDialog = new ProgressDialog(activity);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        */

        pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);

    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
    //  ShortList sendandget = new ShortList();
    //  String resp = sendandget.getResponceFromServer(Appid, null, Uid);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String x="Successfully Rejected";

    //  return resp;
        return x;
    }

    @Override
    protected void onPostExecute(String x) {
        // TODO Auto-generated method stub
         pDialog.dismiss();

}

2 个答案:

答案 0 :(得分:0)

调用AsyncTask后添加dialog.dismiss()

new ShowResponceForAccept().execute();
dialog.dismiss();

答案 1 :(得分:0)

中使用YourActivity.this代替activity

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);

pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);

希望这有帮助。