2011-08-13 4 views
16

중단 점을 설정했지만 무시되거나 보지 못했습니다.어떻게 AsyncTask의 doInBackground 코드를 디버깅 할 수 있습니까?

내 코드는 다음과 같습니다. SQL 데이터베이스를 SD 카드에 백업하려고합니다.

이클립스에서 (디버그 모드가 아닌) 실행했을 때, 나는 onPreExecute로부터 메시지를 받았고 곧바로 onPostExecute의 메시지가 나왔다.

나는 ExportDatabaseFileTask의 거의 모든 줄에 BreakPoints를 설정했습니다.

내가 이클립스 (디버그 모드)를 실행하면, 내가 onPreExecute에서 중단 점에서 중지, 그리고 내가 더 단계로 다음, 바로 다음 라인은 디버거가 간다은 다음과 같습니다

mDbHelper.open() ;

정상 코드의 나머지 부분을 단계별로 실행하고 onPreExecute에서 메시지를 보여주는 AVD와 함께 남았습니다. 분명히 영원히 머무를 것입니다. 나는 정중하게 나는 그것이 breakpointed하지 않아도 주석에 동의해야하거나하지 CopyFile 수

그래서

doInBackground onPostExecute :

나는에 BREAKPOINTED 라인 중 하나를 볼 수 없습니다 실행되고있다. 그래서, 내 질문을 다시 드릴 게요 : 어떻게 당신이 디버깅 (단계) doInBackground 코드 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(); 
      } 
     } 
    } 

}

+0

시도 : –

+0

@Tom Wruble -'Log.d()'지문을'doInBackground'에 추가 했습니까? 그들이 인쇄 되었습니까? – MByD

+0

필자는이 두 줄 사이를 읽고 있지만, AsyncTask의 모든 코드를 단계적으로 수행 할 수는 없으며 디버깅하는 방법은 Log 문을 통해 수행하는 것으로 나타났습니다. 그게 사실이라면, 나는 그것을 처리 할 수있다. 하지만 처음에는 내가 Breakpoints를 설정하지 않았거나 codxe가 실행되지 않았다고 말했고 디버그를 통해 AysyncTask에서 코드를 단계별로 실행할 수 있음을 암시합니다. –

답변

2

는, 그것은 아마도이 두 가지 중 하나 : 당신은 디버그 모드가 아닌 프로젝트를 실행

  1. .
  2. 실행하는 동안 실제로 그 라인에 도달하지 마십시오. 해당 스레드에 중단 점을 설정할 때
4

sargas()을 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.. 
} 

`

0

나는 안드로이드 스튜디오에서 비슷한 직면했다을 추가합니다. 나를 위해, Proguard가 디버깅 중에 활성화 되었기 때문입니다. Proguard는 축소 후 줄 번호를 어지럽 혔습니다. 빌드에서 minfyEnabled true을 제거하여 Proguard를 비활성화 할 수 있습니다.작업에 Log.d()를 넣어

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

Gradle을

다른 비동기 작업이 백그라운드에서 실행되는 경우, 새로운 하나의 방법을 doinbackground 실행 건너과 같이 실행하려고 할 수 있습니다
관련 문제