【问题标题】:Can't call an AsyncTask's execute() method in another AsyncTask's postexecute()不能在另一个 AsyncTask 的 postexecute() 中调用 AsyncTask 的 execute() 方法
【发布时间】:2023-03-13 12:03:01
【问题描述】:

我正在使用内部 AsyncTasks 类来发出服务器请求并根据结果更新我的 UI。是否禁止从另一个 AsyncTask 的 postexecute() 调用 execute() 方法? (我在 RatingHospital() 的 postexecute() 中调用 new updateRating().execute(),但它不起作用:()

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


    JSONParser jsonParser = new JSONParser();

    private static final String RATING_URL = "http://2healthpal.fh2web.com/Server/rating.php";
    private static final String TAG_SUCCESS = "success";
    boolean hasRating;


    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        //Background works              

        return null;
    }

    @Override
    protected void onPostExecute(String result) {



        if (hasRating==true)
            {
              showUpdateAlert();
            }
     else {

             showRatingDialog();

            //TODO
        }
    }

    private void showRatingDialog() {

        final Dialog d = new Dialog(Main.this);
        d.setTitle("User Feedback");
        d.setContentView(R.layout.dialog_rating);

        final RatingBar userRating = (RatingBar) d.findViewById(R.id.ratingBarHosp);
        Button saveRating = (Button) d.findViewById(R.id.btnSaveRating);

        saveRating.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View v) {

                ratingToSave = userRating.getRating();
                d.dismiss();
                new updateRating().execute();  //This line is creating problem
            }
        });


        d.show();

    }

    private void showUpdateAlert() {
        AlertDialog.Builder dBuilder = new AlertDialog.Builder(Main.this);

        dBuilder.setIcon(R.drawable.ic_dialog_alert);
        dBuilder.setTitle("Already Rated!!");
        dBuilder.setMessage("U have already rated this hospital.\n Do you want to update?");
        dBuilder.setCancelable(false);
        dBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        showRatingDialog();
                    }
                });

        dBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog quitAlert = dBuilder.create();
        quitAlert.show();

    }

}

class updateRating extends AsyncTask<String, String, String> {




    @Override
    protected void onPreExecute() {

        super.onPreExecute();

    }

    protected String doInBackground(String... param) {




        return null;
    }


    protected void onPostExecute(String... params) {



        if(success==1)
            Toast.makeText(getApplicationContext(), "Your feedback is saved successfully", Toast.LENGTH_LONG).show();


    }


}

【问题讨论】:

  • 你可以调用它,无论如何发布错误,(logcat)
  • 它会造成什么问题?

标签: java android android-asynctask


【解决方案1】:

所以你可以这样做:

    updateRating data = new updateRating();
    data.execute();

【讨论】:

  • 它有什么不同?
猜你喜欢
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 2021-04-04
  • 1970-01-01
相关资源
最近更新 更多