eclipse 如何调试 AsyncTask 的 doInBackground 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7051157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:50:05  来源:igfitidea点击:

How can I debug doInBackground code of an AsyncTask

androideclipseandroid-asynctask

提问by Tom Wruble

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.

我的代码如下。我正在尝试将 sql db 备份到 SD 卡。

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.

当我在 Eclipse 中运行它(不是调试模式)时,我从 onPreExecute 收到消息,然后很快收到来自 onPostExecute 的消息。

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

我几乎在 ExportDatabaseFileTask 的每一行都设置了 BreakPoints。

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:

当我在 Eclipse 中运行它(在调试模式下)时,我停在 onPreExecute 中的断点处,然后当我更进一步时,调试器转到的下一行是:

mDbHelper.open();

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.

然后我单步执行其余的正常代码,并留下 AVD 显示来自 onPreExecute 的消息,它显然会永远保持下去。

I do NOT see any of the lines BREAKPOINTED in:

我没有看到 BREAKPOINTED 中的任何行:

doInBackground onPostExecute copyFile

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?

所以,我必须恭敬地不同意我没有断点或没有执行的评论。所以,我将重新提出我的问题:你如何调试(逐步)AsyncTask 的 doInBackground 代码?

        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();
            }
        }
    }

}

}

回答by Imon

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

检查这个在为 Android 开发时如何在 AsyncTask 中使用 Eclipse 调试器?

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

将以下代码片段放在开头 doInBackground()

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

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

然后当您在该线程中设置断点时,eclipse 会找到它。

Courtesy: sargas

礼貌: sargas

回答by MByD

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.
  1. 您不是在调试模式下运行项目。
  2. 在执行过程中,您永远不会真正到达这些行。

回答by Hardian

In your doInBackground() add the following..`

在您的 doInBackground() 中添加以下内容...`

    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..
}

`

`

回答by Nabeel

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 truefrom your build.gradle

我在 Android Studio 中遇到了类似的问题。对我来说,这是由于 Proguard 在调试期间处于活动状态。Proguard 在缩小后弄乱了行号。您可以通过minfyEnabled true从 build.gradle 中删除来禁用 Proguard

回答by Mukesh Singh

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

如果另一个异步任务在后台运行,那么可能是新的一个跳过运行 doinbackground 方法,所以尝试运行如下:

(new ExampleAsyncTask()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);