0

I'm trying to run this code in AsyncTask doInBackground but in the application closes and throws me an error in the log which does not mean that.

Earlier execution of AlertDialog was within the OnPostExecute function but I cast a warning: "The application May be doing too much work on its main thread." and sometimes the application is closed.

For this reason I decided to do so within the doInBackground but as I wrote above I get an error which does not mean that.

Any help please?

Thank you

function:

//AsyncTask para insertar Personas
        class Insertar extends AsyncTask<String,String,String>{

            private Activity context;

            Insertar(Activity context){
                this.context=context;
            }
            @Override
            protected String doInBackground(String... params) {
              // TODO Auto-generated method stub


              String result = insertar();

              if ( result.equals("0") ){

                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); 
                    alert.setTitle("Dona1Click - Patrocinador");

                    alert.setMessage("Lo sentimos, ha excedido el limite diaro de 20 clicks");

                    alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });

                    alert.show();


                }


                if ( result.equals("1") ){

                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); 
                    alert.setTitle("Dona1Click - Patrocinador");


                    WebView wv = new WebView(getActivity());
                    wv.loadUrl("http:\\www.google.com");
                    wv.setWebViewClient(new WebViewClient() {

                        public void onPageFinished(WebView view, String url) {
                            if(pd.isShowing()&&pd!=null)
                            {
                                pd.dismiss();
                            }
                        }


                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            view.loadUrl(url);

                            return false;
                        }
                    });

                    alert.setView(wv);
                    alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });

                    alert.show();

                 // Para colocar un loading

                    pd = ProgressDialog.show(getActivity(), "", "Loading...",true);

                    //--------------------------



                }



              return null;
            }


            @Override
            protected void onPostExecute(String result) {


            }

log:

08-04 18:01:12.221: D/dalvikvm(1229): GC_FOR_ALLOC freed 39K, 5% free 2808K/2928K, paused 82ms, total 86ms
08-04 18:01:12.221: I/dalvikvm-heap(1229): Grow heap (frag case) to 4.604MB for 1872016-byte allocation
08-04 18:01:12.391: D/dalvikvm(1229): GC_FOR_ALLOC freed <1K, 3% free 4636K/4760K, paused 145ms, total 145ms
08-04 18:01:13.381: D/gralloc_goldfish(1229): Emulator without GPU emulation detected.
08-04 18:01:18.251: D/dalvikvm(1229): GC_FOR_ALLOC freed 94K, 4% free 4863K/5040K, paused 356ms, total 359ms
08-04 18:01:18.251: I/dalvikvm-heap(1229): Grow heap (frag case) to 5.431MB for 635812-byte allocation
08-04 18:01:18.481: D/dalvikvm(1229): GC_FOR_ALLOC freed <1K, 4% free 5483K/5664K, paused 222ms, total 222ms
08-04 18:01:19.551: I/ActionBar(1229): null seleccionada.
08-04 18:01:19.951: D/dalvikvm(1229): GC_FOR_ALLOC freed 11K, 3% free 5571K/5704K, paused 311ms, total 315ms
08-04 18:01:19.961: I/dalvikvm-heap(1229): Grow heap (frag case) to 7.714MB for 2304016-byte allocation
08-04 18:01:20.231: D/dalvikvm(1229): GC_FOR_ALLOC freed <1K, 2% free 7821K/7956K, paused 269ms, total 269ms
08-04 18:01:23.321: I/Choreographer(1229): Skipped 70 frames!  The application may be doing too much work on its main thread.
08-04 18:01:23.851: I/Choreographer(1229): Skipped 42 frames!  The application may be doing too much work on its main thread.
08-04 18:01:26.331: V/WebViewChromium(1229): Binding Chromium to the background looper null
08-04 18:01:26.331: W/dalvikvm(1229): threadid=11: thread exiting with uncaught exception (group=0xb3aa1ba8)
08-04 18:01:26.361: E/AndroidRuntime(1229): FATAL EXCEPTION: AsyncTask #1
08-04 18:01:26.361: E/AndroidRuntime(1229): Process: com.example.dona1click, PID: 1229
08-04 18:01:26.361: E/AndroidRuntime(1229): java.lang.RuntimeException: An error occured while executing doInBackground()
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at java.lang.Thread.run(Thread.java:841)
08-04 18:01:26.361: E/AndroidRuntime(1229): Caused by: java.lang.NullPointerException
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.os.Handler.<init>(Handler.java:229)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.os.Handler.<init>(Handler.java:137)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at com.android.org.chromium.base.ThreadUtils.setUiThread(ThreadUtils.java:34)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at com.android.webview.chromium.WebViewChromiumFactoryProvider.ensureChromiumStartedLocked(WebViewChromiumFactoryProvider.java:104)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at com.android.webview.chromium.WebViewChromiumFactoryProvider.startYourEngines(WebViewChromiumFactoryProvider.java:259)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:218)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.webkit.WebView.<init>(WebView.java:508)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.webkit.WebView.<init>(WebView.java:475)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.webkit.WebView.<init>(WebView.java:455)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.webkit.WebView.<init>(WebView.java:444)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.webkit.WebView.<init>(WebView.java:434)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at com.example.dona1click.Donar$Insertar.doInBackground(Donar.java:178)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at com.example.dona1click.Donar$Insertar.doInBackground(Donar.java:1)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-04 18:01:26.361: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-04 18:01:26.361: E/AndroidRuntime(1229):     ... 4 more
08-04 18:01:27.891: I/Choreographer(1229): Skipped 344 frames!  The application may be doing too much work on its main thread.
08-04 18:01:30.671: I/Choreographer(1229): Skipped 62 frames!  The application may be doing too much work on its main thread.
08-04 18:01:32.691: I/Process(1229): Sending signal. PID: 1229 SIG: 9

2 Answers2

0

You can't change anything view in doInBackground, for that you must use onProgressUpdate or onPostExecute

lacrirra
  • 98
  • 1
  • 2
  • if i change view in onPostExecute appear this: The application May be doing too much work on its main thread. –  Aug 05 '14 at 13:59
0

You can't do UI operations on Background thread. You have to do UI operation on onProgressUpdate or onPostExecute. For using OnProgressUpdate you have to give a call to onProgressUpdate by calling publishProgress(); from doInBackground.

However there is a method runOnUiThread. This Link explains how to use that.

Community
  • 1
  • 1
MrDumb
  • 2,140
  • 1
  • 21
  • 31
  • if i do UI operations in onPostExecute appear this: The application May be doing too much work on its main thread. –  Aug 05 '14 at 13:58
  • The code you have written there should be on Main thread only cause they are dealing with the UI operations. However if you app is using bitmaps or network operation, that can be written in Async Task. If you app is getting forced close often you can set largeHeap = "true"; in your manifest file under Application tag. – MrDumb Aug 06 '14 at 04:38