Home Getting error while executing AsyncTask in Android App
Reply: 1

Getting error while executing AsyncTask in Android App

Mohammad Shamshuddeen
1#
Mohammad Shamshuddeen Published in 2017-12-07 21:29:11Z

I am Getting an error java.lang.RuntimeException: An error occurred while executing doInBackground() while executing

here is my doInBackground() method

 @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;
    }

here is the method where i am calling 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();




    }

here is the 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) 
colens
2#
colens Reply to 2017-12-07 22:51:57Z

Your doInBackground is not executing on the UI thread but on the different thread and you can't touch views created on UI thread from background thread. Also, in your AsyncTask you are not doing anything that needs to be done in the background, so you can move all the stuff to your activity which runs on the UI thread.

Main reason for using AsyncTask is some lengthy job that would freeze your UI if it's done on the main thread. If you are just dealing with your views and performing short tasks then leave it on the main thread.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.320139 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO