Asynctask有时会崩溃

时间:2014-01-08 07:54:53

标签: android multithreading android-listview android-asynctask

我正在开发一个应用程序,它从Web服务获取一些数据并显示在列表视图中。我使用自定义适配器的列表视图,它由Base Adapter类扩展。我已经使用Assync Task执行某些操作,例如调用Web服务并从中获取响应并在Toast中显示(在我的列表视图中,每行有两个按钮[Accept and Reject]。当有人点击Accept it调用Web服务)。

所以我在Adapter类中实现了我的assync任务。

我的问题是:当我点击列表按钮时,有时我的应用程序崩溃。[运行那些同步任务时]。但有时它可以正常工作。

错误类似于:

01-08 12:20:26.919: E/AndroidRuntime(21754): FATAL EXCEPTION: AsyncTask #3
01-08 12:20:26.919: E/AndroidRuntime(21754): java.lang.RuntimeException: An error occured while executing doInBackground()
01-08 12:20:26.919: E/AndroidRuntime(21754):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at java.lang.Thread.run(Thread.java:856)
01-08 12:20:26.919: E/AndroidRuntime(21754): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
01-08 12:20:26.919: E/AndroidRuntime(21754):    at android.os.Looper.prepare(Looper.java:78)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at android.os.Looper.prepare(Looper.java:73)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at com.jsonlist.jsonlist.NewsRowAdapter$ShowResponceForReject.doInBackground(NewsRowAdapter.java:646)
01-08 12:20:26.919: E/AndroidRuntime(21754):    at com.jsonlist.jsonlist.NewsRowAdapter$ShowResponceForReject.doInBackground(NewsRowAdapter.java:1)

谁能告诉我这个bug在哪里?

这些是它所指的代码

        public void dialogshow(final String appid){

        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) {



                //  new ShowResponceForAccept().execute("");
                    //dialog.dismiss();
                    Handler mHandler=new Handler();

                    mHandler.post(new Runnable() 
                    {
                         public void run()
                         {
                              new ShowResponceForAccept().execute(appid);


                         }
                    });



                /*  ShortList sendandget = new ShortList();
                    //long appoinmentID = Long.valueOf(appid);
                    //long user = Long.valueOf(Uid);
                    String resp = sendandget.getResponceFromServer(appid,"",Uid);
                    Appid = null;
                    Uid = null;
                    //Toast.makeText(mContext, resp, Toast.LENGTH_LONG).show();
                    loadListAgain();*/



                }
              })
              .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        dialog.cancel();
                    }
                });
        alertDialogBuilder.show();

    }

这是同步任务,位于同一个适配器类

    public class ShowResponceForAccept extends AsyncTask<String, Void, 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);

    }

    @SuppressWarnings("static-access")
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        Looper.myLooper().prepare(); 
        ShortList sendandget = new ShortList();
        String app = arg0[0];
        //long appoinmentID = Long.valueOf(app);
        //long user = Long.valueOf(Uid);
        String resp = sendandget.getResponceFromServer(app,"",Uid);
        loadListAgain();




        //Thread.sleep(10000);
        //String x="Your Request Accepted";

        return resp;
        //return x;
    }

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

         if(resp.equalsIgnoreCase("true")){
             Toast.makeText(mContext, "Your Request has been Successfully Accepted!", Toast.LENGTH_LONG).show();

         }
         else{
             Toast.makeText(mContext, "Sorry! Try again Later!", Toast.LENGTH_LONG).show();
         }


    }


}

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

问题在于

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

在此方法中,您的调用

  Looper.myLooper().prepare(); 

您应该从doInBackground中删除UI更新代码并在postExecute中将其推送并删除Looper。那应该解决问题

答案 1 :(得分:0)

问题就在那里,

您正尝试在AsyankTask执行之间打开Dialog。当AsysnkTask运行时,不要更新UI,更新onPostExecute。