【问题标题】:How can I debug doInBackground code of an AsyncTask如何调试 AsyncTask 的 doInBackground 代码
【发布时间】:2017-09-15 17:40:54
【问题描述】:

我设置了断点,但它们似乎被忽略(或从未见过)。

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

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

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

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

mDbHelper.open();

然后,我将逐步执行其余的正常代码,并留下显示来自 onPreExecute 的消息的 AVD,它显然会永远存在。

我没有看到任何 BREAKPOINTED 行:

doInBackground 后执行 复制文件

所以,我必须恭敬地不同意我没有断点或没有被执行的评论。那么,我将重新提出我的问题:您如何调试(STEP THROUGH)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();
            }
        }
    }

}

【问题讨论】:

  • 尝试在任务中放一个Log.d()
  • @Tom Wruble - 您是否将Log.d() 打印添加到doInBackground?打印出来了吗?
  • 我在字里行间阅读,但看起来你(虽然没有真正说出来)无法单步执行 AsyncTask 中的所有代码,并且调试它的唯一方法是通过日志声明。如果这是真的,很酷,我可以处理它。但一开始你说我要么没有设置断点,要么没有执行代码库,这当然意味着你可以通过调试单步执行 AysyncTask 中的代码。
  • 有趣...我之前已经对AsyncTask.doInBackground 进行了调试,方法是在其中设置断点然后单步执行。 Eclipse 调试器不仅能够调试多线程应用程序。最有可能的是,源代码在某处与编译后的代码不同步。我建议在 Eclipse 中进行“清理”,然后在调试模式下运行之前重新构建项目。还要检查断点是否未被禁用(在调试视图中)。
  • 我刚刚发现,如果我在doInBackground() 中添加一些Log.d(),调试器会突然开始遇到断点!!

标签: android eclipse android-asynctask


【解决方案1】:

检查这个How do I use the Eclipse debugger in an AsyncTask when developing for Android?

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

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

那么当你在那个线程中设置断点时,eclipse会找到它。

礼貌:萨加斯

【讨论】:

    【解决方案2】:

    如果它们被忽略,那么它可能是这两个之一:

    1. 您不是在调试模式下运行项目。
    2. 在执行过程中您永远不会真正到达这些行。

    【讨论】:

    • 不敢相信我忘记了.execute()。 +1
    【解决方案3】:

    在你的 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..
    }
    

    `

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 2015-11-08
          • 2016-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多