0

doInbackGround 内で CountDownTimer を作成しようとしていますが、このエラーが返されます。これを doInBackGround の中に入れられないのはなぜですか。私はこれをトレッドの最後に実行するために行っています。

この Asynctask を 2 回呼び出す必要があります。初めて呼び出すと、変数 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();
}

}

4

1 に答える 1