2017-05-13 80 views
0

我有一個定期執行任務的asynctask。有用。我已經測試過無數次。但是,由於某些原因,它偶爾會不時崩潰。由於堆棧跟蹤非常模糊,我無法找到問題的根源。這裏的堆棧跟蹤:AsyncTask導致NoClassDefFoundError

Fatal Exception: java.lang.RuntimeException: An error occured while executing doInBackground() 
     at android.os.AsyncTask$3.done(AsyncTask.java:299) 
     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
     at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
     at java.lang.Thread.run(Thread.java:856) 
Caused by java.lang.NoClassDefFoundError: android.system.ErrnoException 
     at dita.dev.myportal.SyncManager$GetData.doInBackground(Unknown Source) 
     at dita.dev.myportal.SyncManager$GetData.doInBackground(Unknown Source) 
     at android.os.AsyncTask$2.call(AsyncTask.java:287) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
     at java.lang.Thread.run(Thread.java:856) 

起初我還以爲是因爲我使用ProGuard和jsoup,但我加入jsoup規則後測試,它的工作原理。這裏是我的規則jsoup:

-keeppackagenames org.jsoup.nodes 
-keep public class org.jsoup.** { 
    public *; 
} 

這裏是正在執行的任務:

private static class GetData extends AsyncTask<Void, Integer, Boolean> { 

     private Context context; 
     private String message; 

     GetData(Context context) { 
      this.context = context; 
     } 

     @Override 
     protected Boolean doInBackground(Void... voids) { 
      String username = Preferences.getString(context, "username"); 
      String password = Preferences.getString(context, "password"); 
      if (username == null) { 
       return false; 
      } 
      PortalCrawler crawler = new PortalCrawler(Config.BASE_URL1); 

      try { 
       crawler.start(username, password); 
       crawler.saveData(); 
       return true; 
      } catch (Exception e) { 
       if (e instanceof SocketTimeoutException || e instanceof UnknownHostException || 
         e instanceof ErrnoException || e instanceof SocketException) { 
        crawler.setBaseUrl(Config.BASE_URL2); 
        try { 
         crawler.start(username, password); 
         crawler.saveData(); 
         return true; 
        } catch (Exception e1) { 
         e1.printStackTrace(); 

         if (e1 instanceof SocketTimeoutException || e1 instanceof UnknownHostException 
           || e1 instanceof ErrnoException || e1 instanceof SocketException) { 
          message = "Unable to connect"; 
         } else { 
          Crashlytics.logException(e.getCause()); 
          message = "An error occurred"; 
         } 
        } 

       } else { 
        e.printStackTrace(); 
        Crashlytics.logException(e.getCause()); 
        message = "An error occurred"; 
       } 

      } 
      return false; 
     } 

     @Override 
     protected void onPostExecute(Boolean success) { 
      if (success) { 
       Preferences.setBoolean(context, "initialSync", true); 
       message = "Sync completed"; 
      } 
      EventBus.getDefault().post(new Events.SyncDone(success, message)); 
     } 
    } 
} 

回答

1

我相信這個問題是在這裏:

e instanceof ErrnoException

我懷疑你正試圖運行此應用程序在預先API-21設備上,其中android.system.ErrnoException不存在。它是在21中引入的。

您將其編譯爲21後的API,然後嘗試在21前設備上運行它。

+0

我沒有注意到這一點。那一定是吧。謝謝。你如何建議我抓住它。原因支持前棒棒糖設備 – Michael

+0

http://stackoverflow.com/questions/3993924/get-android-api-level-of-phone-currently-running-my-application –

相關問題