Home » Android » android – Application Freezes when doing queries Inside background Thread-Exceptionshub

android – Application Freezes when doing queries Inside background Thread-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment


I have an Application that Doing a lot of Queries. But I’m Executing these queries inside a background thread to avoid freezing or (Appplication Not Responding)ANR.

But the background thread run inside OnclickListener and I’ve just learned that ListenerCallbacks always run in the UI or Main Thread which only means that my queries still runs in the Main thread tho its inside a background Thread. That is why My app still freezes.

Question is How can I fix this?

I tried AsyncTask But it also doesn’t work

AsyncTask.execute(new Runnable() {
        public void run() {


this is my log just to add information

E/Surface: dequeueBuffer failed (Function not implemented)
E/ViewRootImpl[ManageReading]: Could not lock surface
        at android.view.Surface.nativeLockCanvas(Native Method)
        at android.view.Surface.lockCanvas(Surface.java:331)
        at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:3549)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:3515)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3271)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2766)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1650)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
        at android.view.Choreographer.doCallbacks(Choreographer.java:732)
        at android.view.Choreographer.doFrame(Choreographer.java:664)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:202)
        at android.app.ActivityThread.main(ActivityThread.java:6962)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
How to&Answers:

I strongly recommend using Kotlin Coroutines. But if you choose to stick with AsyncTask, try something like this:

//Create an AsyncTask class 
//Pay attention to the parameter types. You probably want to change them.
    private class QueryTask extends AsyncTask<String, Integer, Boolean> {
     protected Boolean doInBackground(String... queries) {
        // Execute queries here

        //Obtain intermediate updates

       // return a boolean or whatever makes sense


     protected void onProgressUpdate(Integer... progress_percent) {
           //update UI with the intermediate updates              


     protected void onPostExecute(Long result) {
         // Do something on completion

// Create an instance of QueryTask and invoke execute
new QueryTask().execute(query_param1, query_param2, query_param3);