试图在doInBackGroung中创建countDownTimer

时间:2013-10-04 12:51:25

标签: android android-asynctask

我试图在doInbackGround中创建一个CountDownTimer但是它为我返回了这个错误。为什么我不能把它放在doInBackGround中。我这样做是为了延长执行时间。

我不得不拨打2次这个Asynctask。当我第一次调用它时,它不会更新变量retornoServidor。我认为没有时间执行http.postData。

10-04 09:48:59.719: E/AndroidRuntime(18894): FATAL EXCEPTION: AsyncTask #1
10-04 09:48:59.719: E/AndroidRuntime(18894): java.lang.RuntimeException: An error occured while executing doInBackground()
10-04 09:48:59.719: E/AndroidRuntime(18894):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.lang.Thread.run(Thread.java:1019)
10-04 09:48:59.719: E/AndroidRuntime(18894): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-04 09:48:59.719: E/AndroidRuntime(18894):    at android.os.Handler.<init>(Handler.java:121)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at android.os.CountDownTimer$1.<init>(CountDownTimer.java:109)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at android.os.CountDownTimer.<init>(CountDownTimer.java:109)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at br.com.c.Helper.AsyncTaskCodigo$1.<init>(AsyncTaskCodigo.java:53)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at br.com.asdf.Helper.AsyncTaskCodigo.doInBackground(AsyncTaskCodigo.java:53)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at br.com.sdfs.Helper.AsyncTaskCodigo.doInBackground(AsyncTaskCodigo.java:1)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-04 09:48:59.719: E/AndroidRuntime(18894):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-04 09:48:59.719: E/AndroidRuntime(18894):    ... 4 more
10-04 09:49:06.089: E/WindowManager(18894): Activity br.com.sdf.sdf.Activities.TelaCodigoActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40684d20 that was originally added here
10-04 09:49:06.089: E/WindowManager(18894): android.view.WindowLeaked: Activity br.com.sdf2.sdf.Activities.TelaCodigoActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40684d20 that was originally added here
10-04 09:49:06.089: E/WindowManager(18894):     at android.view.ViewRoot.<init>(ViewRoot.java:261)
10-04 09:49:06.089: E/WindowManager(18894):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:170)
10-04 09:49:06.089: E/WindowManager(18894):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:113)
10-04 09:49:06.089: E/WindowManager(18894):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-04 09:49:06.089: E/WindowManager(18894):     at android.app.Dialog.show(Dialog.java:241)
10-04 09:49:06.089: E/WindowManager(18894):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
10-04 09:49:06.089: E/WindowManager(18894):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
10-04 09:49:06.089: E/WindowManager(18894):     at br.com.sdf.Helper.AsyncTaskCodigo.onPreExecute(AsyncTaskCodigo.java:36)
10-04 09:49:06.089: E/WindowManager(18894):     at android.os.AsyncTask.execute(AsyncTask.java:391)
10-04 09:49:06.089: E/WindowManager(18894):     at br.com.sdf.sdf.Activities.TelaCodigoActivity$1.onClick(TelaCodigoActivity.java:72)
10-04 09:49:06.089: E/WindowManager(18894):     at android.view.View.performClick(View.java:2485)
10-04 09:49:06.089: E/WindowManager(18894):     at android.view.View$PerformClick.run(View.java:9080)
10-04 09:49:06.089: E/WindowManager(18894):     at android.os.Handler.handleCallback(Handler.java:587)
10-04 09:49:06.089: E/WindowManager(18894):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 09:49:06.089: E/WindowManager(18894):     at android.os.Looper.loop(Looper.java:130)
10-04 09:49:06.089: E/WindowManager(18894):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-04 09:49:06.089: E/WindowManager(18894):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 09:49:06.089: E/WindowManager(18894):     at java.lang.reflect.Method.invoke(Method.java:507)
10-04 09:49:06.089: E/WindowManager(18894):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
10-04 09:49:06.089: E/WindowManager(18894):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
10-04 09:49:06.089: E/WindowManager(18894):     at dalvik.system.NativeStart.main(Native Method)

代码

public class AsyncTaskCodigo extends AsyncTask<String, Integer, String>{

Context context;    
ProgressDialog progress;
String codigo;
long idUsuario; 
HttpRequest http = new HttpRequest();


public AsyncTaskCodigo(Context conetex, String codigo, long idUsuario){
    this.context = conetex;
    this.codigo = codigo;
    this.idUsuario = idUsuario;
}


@Override
protected void onPreExecute() {
     progress = ProgressDialog.show(context, "Aguarde!", "Verificando o seu cófdigo...", true);     

}


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

    JSONObject json = new JSONObject();

    try{

        json.put("Codigo", codigo);
        json.put("Id_Usuario", idUsuario);

        http.postData(http.getUrltesteusuariocodigo(), json);
        publishProgress(1);

        if(http.temp.trim().equals("Yes")){
            TelaCodigoActivity.retornoServidor = "Yes";
        } else {
            TelaCodigoActivity.retornoServidor = "No";                                                  
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }

    return "ok";
}

@Override
protected void onPostExecute(String result) {

    try {
        progress.dismiss();
        progress = null;
    } catch (Exception e) {
        e.printStackTrace();
    }

}


@Override
protected void onProgressUpdate(Integer... values) {
     new CountDownTimer(3000,1000) {                                        
          @Override
          public void onFinish() {

          }

          @Override
          public void onTick(long millisUntilFinished) {

          }

      }.start();
}

}

1 个答案:

答案 0 :(得分:0)

只需将此计数器代码放在onProgressUpdate()方法中即可。并在doInbackground()中调用publishProgress()。您的代码将如下所示

@Override
 protected String doInBackground(Object... arg0) {

JSONObject json = new JSONObject();

try{

    json.put("Codigo", codigo);
    json.put("Id_Usuario", idUsuario);

    http.postData(http.getUrltesteusuariocodigo(), json);
    publishProgress();


    if(http.temp.toString().trim().equals("Yes")){
        TelaCodigoActivity.retornoServidor = "Yes";
    } else {
        TelaCodigoActivity.retornoServidor = "No";
    }


} catch (JSONException e) {
    e.printStackTrace();
}

return "ok";
}

@Override
 protected void onPostExecute(String result) {

try {
    progress.dismiss();
    progress = null;
} catch (Exception e) {
    e.printStackTrace();
}

}

   @Override
protected void onProgressUpdate(String... values) {
    new CountDownTimer(3000, 1000) {                                        
        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub

        }

    }.start();
}