如何调试doInBackground $ C $的的AsyncTask了C [英] How can I debug doInBackground code of an AsyncTask
问题描述
我有断点设置,但他们似乎忽略了(或者从来没见过)。
I have breakpoints set, but they seem to be ignore (or never seen).
我的code是如下。我想备份SQL数据库到SD卡。
My code is below. I'm trying to backup a sql db to an SD card.
当我在Eclipse中运行它(不调试模式下),我得到的消息来自于preExecute,随后不久从onPostExecute消息。
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.
我设置断点几乎ExportDatabaseFileTask的每一行。
I have BreakPoints set in almost every line of the ExportDatabaseFileTask.
当我运行它(在调试模式下)在Eclipse中,我停在断点在preExecute,然后我再走一步,在下一个线路调试器去是:
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();
mDbHelper.open();
我再踩通过正常的code中的休息,我离开了问心无愧的AVD显示从上preExecute,wher它显然将永远留消息。
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.
我看不到任何BREAKPOINTED在行:
I do NOT see any of the lines BREAKPOINTED in:
doInBackground onPostExecute 的CopyFile
doInBackground onPostExecute copyFile
所以,我必须恭敬地与我没有它breakpointed或没有被执行的意见分歧。所以,我会reask我的问题:你如何调试(单步)的的AsyncTask的doInBackground code
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();
}
}
}
}
推荐答案
检查该<一href="http://stackoverflow.com/questions/4770587/how-do-i-use-the-eclipse-debugger-in-an-asynctask-when-developing-for-android">How做开发的机器人,当我使用Eclipse调试器中的AsyncTask?
将下面的code片段doInBackground的开头:
Put the following code fragment in the beginning of doInBackground:
android.os.Debug.waitForDebugger();
android.os.Debug.waitForDebugger();
然后,当你在一个线程中设置一个断点,日食会发现它。
Then when you set a breakpoint in that thread, eclipse will find it.
礼貌:sargas
这篇关于如何调试doInBackground $ C $的的AsyncTask了C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!