2015年8月4日 星期二

[Android] AsyncTask 使用 Toast

在 #doInBackground() 抓檔時,會想要即時用 Toast 顯示訊息。 但是會造成錯誤:

java.lang.RuntimeException: An error occured while executing doInBackground()
...

可行的方法是在 #postExecute() 才處理。

private Exception exception;

@Override
protected Result doInBackground(Params... requests) {
   try {
   } catch (Exception e) {
      // Toast.makeText(context, "error occurs", Toast.LENGTH_LONG).show();
      exception = e;
   }
}

@Override
protected void onPostExecute(List googleImages) {
   if (exception != null) {
      Toast.makeText(context, "error occurs", Toast.LENGTH_LONG).show();
   }
}

參考資料:StackOverflow: java.lang.RuntimeException: An error occured while executing doInBackground()

Android Studio 1.3
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc3"

沒有留言:

張貼留言

[Java] Invalid HTTP method: PATCH

最近系統需要使用 Netty4,所以把衝突的 Netty3 拆掉,然後就出現了例外。 pom.xml <dependency> <groupId>com.ning</groupId> <artifactId>as...