在Android App中执行AsyncTask时出错

时间:2017-12-07 21:29:12

标签: java android android-asynctask

我收到错误java.lang.RuntimeException:执行时执行doInBackground()时发生错误

这是我的doInBackground()方法

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


            AlertDialog.Builder rewardedAdvert = new AlertDialog.Builder(PlayGame.this);
            View rView = getLayoutInflater().inflate(R.layout.rewardedads, null);
            homeButton = (Button) rView.findViewById(R.id.homebutton);


            homeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    gameOver();
                }
            });
            rewardedAdvert.setView(rView);

            AlertDialog dialog = rewardedAdvert.create();
          dialog.getWindow().setLayout(700, 700);


        return null;
    }

这是我调用AsyncTask

的方法
  private void wrongAnswer() {
    correctImg.setVisibility(View.INVISIBLE);
    wrongImg.setVisibility(View.VISIBLE);
    ((LightQuiz) this.getApplicationContext()).soundHandler.playWrongSound();
    lives--;
    if (lives == 0) {

       new GameOverRewardedAds().execute();

      //  gameOver();




    }

这是logcat:

Java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                            at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                            at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                            at java.lang.Thread.run(Thread.java:818)
                                                                         Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                                            at android.os.Handler.<init>(Handler.java:200)
                                                                            at android.os.Handler.<init>(Handler.java:114)
                                                                            at android.app.Dialog.<init>(Dialog.java:119)
                                                                            at android.app.AlertDialog.<init>(AlertDialog.java:200)
                                                                            at android.app.AlertDialog$Builder.create(AlertDialog.java:1086)
                                                                            at com.awwalsoft.javaplay.PlayGame$GameOverRewardedAds.doInBackground(PlayGame.java:429)
                                                                            at com.awwalsoft.javaplay.PlayGame$GameOverRewardedAds.doInBackground(PlayGame.java:387)
                                                                            at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                            at java.lang.Thread.run(Thread.java:818) 

1 个答案:

答案 0 :(得分:0)

您的 doInBackground 未在UI线程上执行,但在不同的线程上,您无法触及从后台线程在UI线程上创建的视图。此外,在 AsyncTask 中,您没有做任何需要在后台完成的事情,因此您可以将所有内容移动到在UI线程上运行的活动。

使用AsyncTask的主要原因是一些冗长的工作,如果在主线程上完成,它将冻结你的UI。如果您只是处理您的视图并执行简短任务,请将其保留在主线程上。