21

I have breakpoints set, but they seem to be ignore (or never seen).

My code is below. I'm trying to backup a sql db to an SD card.

When I RUN it (not Debug mode) in eclipse, I get the message from the onPreExecute and followed shortly by the message from the onPostExecute.

I have BreakPoints set in almost every line of the ExportDatabaseFileTask.

When I RUN it (in Debug mode) in eclipse, I stop at the breakpoints in onPreExecute, and then as I step further, THE VERY NEXT LINE the Debugger goes to is:

mDbHelper.open();

I then step through the rest of the normal code and am left wth the AVD showing the message from the onPreExecute, wher it will apparently STAY FOREVER.

I do NOT see any of the lines BREAKPOINTED in:

doInBackground onPostExecute copyFile

So, I have to respectfully disagree with the comment that I don't have it breakpointed or it is not being executed. So, I'll reask my question: How do you debug (STEP THROUGH) doInBackground code of an AsyncTask?

        case BACKUP_ID:
            if (ScoreAGame.this.isExternalStorageAvail()) {
                mDbHelper.close();
                new ExportDatabaseFileTask().execute();
                mDbHelper.open();
            } else {
                Toast.makeText(ScoreAGame.this, "External storage is not available, unable to export data.",
                           Toast.LENGTH_SHORT).show();
            }
            return true;
        case RESTORE_ID:
            selectCourse();
            return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

private boolean isExternalStorageAvail() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(ScoreAGame.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Exporting database...");
        this.dialog.show();
    }

    // automatically done on worker thread (separate from UI thread)
    protected Boolean doInBackground(final String... args) {

        File dbFile =
            new File(Environment.getDataDirectory() + "/data/com.discgolf/databases/discgolfdb.db");

        File exportDir = new File(Environment.getExternalStorageDirectory(), "discgolfbackup");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }
        File file = new File(exportDir, dbFile.getName());

        try {
            file.createNewFile();
            this.copyFile(dbFile, file);
            return true;
        }
        catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
            return false;
        }
    }

    // can use UI thread here
    protected void onPostExecute(final Boolean success) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        if (success) {
            Toast.makeText(ScoreAGame.this, "Export successful!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(ScoreAGame.this, "Export failed", Toast.LENGTH_SHORT).show();
        }
    }

    void copyFile(File src, File dst) throws IOException {
        FileChannel inChannel = new FileInputStream(src).getChannel();
        FileChannel outChannel = new FileOutputStream(dst).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        }
        finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }

}

Tom Wruble
  • 213
  • 1
  • 2
  • 5
  • Try to put a Log.d() in the task – Stefan Bossbaly Aug 13 '11 at 15:01
  • @Tom Wruble - did you add `Log.d()` prints to `doInBackground`? were they printed? – MByD Aug 13 '11 at 15:58
  • I'm reading in between the lines, but it appears that you (while not actuially saying it) cannot step through all code in an AsyncTask, and that the onbly way to debug it it is via the Log statement. If that's true, cool, I can deal with it. But at first you said that I had either not set Breakpoints or that the codxe was not executed, which certainly IMPLIES that you CAN step through code in an AysyncTask via Debug. – Tom Wruble Aug 13 '11 at 17:46
  • Interesting... I have done debugging of `AsyncTask.doInBackground` before by setting breakpoints in it and then stepping through. Eclipse debugger is more than capable of debugging multithreaded applications. Most likely, the source code is not synchronised somewhere with the compiled code. I suggest doing "clean" in Eclipse and then rebuilding the project before running it in debugging mode. Also check that the breakpoints are not disabled (in Debug view). – Aleks G Aug 23 '11 at 13:26
  • I just discovered that if I put some `Log.d()` within `doInBackground()` that the debugger suddenly starts hitting breakpoints !! – Someone Somewhere Oct 09 '14 at 20:26

5 Answers5

43

check this How do I use the Eclipse debugger in an AsyncTask when developing for Android?

Put the following code fragment in the beginning of doInBackground()

if(android.os.Debug.isDebuggerConnected())
    android.os.Debug.waitForDebugger();

Then when you set a breakpoint in that thread, eclipse will find it.

Courtesy: sargas

Community
  • 1
  • 1
Imon
  • 3,905
  • 4
  • 25
  • 25
6

If they are ignored, then it's probably one of this two:

  1. You run the project not in debug mode.
  2. You never really reach those lines during execution.
MByD
  • 135,866
  • 28
  • 264
  • 277
4

In your doInBackground() add the following..`

    protected Integer doInBackground(String... params) {
    // for debug worker thread
    if(android.os.Debug.isDebuggerConnected())
        android.os.Debug.waitForDebugger();

    // create the file with the given file path
    File file = new File(params[0]);

    // enter rest of the code here..
}

`

Hardian
  • 1,922
  • 22
  • 23
1

I was facing something similar in Android Studio. For me, it was due to Proguard being active during debug. Proguard was messing up the line numbers after minifying. You can disable Proguard by removing minfyEnabled true from your build.gradle

Nabeel
  • 53
  • 1
  • 9
1

If another Async Task is running in the background, then may be new one skip to run doinbackground method, so try to run like:

(new ExampleAsyncTask()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Martin Evans
  • 45,791
  • 17
  • 81
  • 97