【发布时间】: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