2011-06-01 107 views
1

我有一個應用程序,用於從服務器加載圖像。因此,我的應用程序導致Outofmemory錯誤。 我已經發現了這個異常,以防止我的應用程序被強制關閉。但我的應用程序停止在發生異常的地方加載圖像。那麼有什麼辦法可以在異常被捕獲後重新開始我的活動,這樣我的內存就可以釋放,活動再次從第一個加載圖像。如何在發生異常後重新啓動活動

非常需要。任何幫助表示讚賞。

回答

0

突然重新開始當前的活動不會是一個很好的用戶體驗。 如果可能,請嘗試從內存中清除不會向用戶顯示的圖像(以釋放它佔用的內存)。

如果你仍然要重新啓動的電流活動,使用以下命令:

void restartActivity() 
{ 
    CurrentActivity.this.finish(); 
    Intent mIntent = new Intent(CurrentActivity.this, CurrentActivity.class); 
    startActivity(CurrentActivity); 
} 
+0

'從內存中清除圖像'我如何實現這一點。這是我真正想要的。我試過system.gc(),bitmap.recycle()。但是我的問題沒有解決。你能提出一個想法嗎? – 2011-06-01 06:52:47

+0

您可以更具體地瞭解您是如何在應用程序中實現圖像的?如果您使用的是listView,請清除列表中所有圖像(通過使用位圖= null),除了那些當前顯示給用戶(即)的內容從第一個可見位置到最後一個可見位置,並在內存不足時調用System。 gc()... – 66CLSjY 2011-06-01 17:15:47

+0

不適用於我:( – SegFault 2013-11-27 16:47:13

0

你不應該捕捉一般的錯誤和特別是OutOfMemoryError。 OOM之後,你永遠無法確定你的應用程序的狀態是什麼。

相反,您應該考慮改進算法,以避免OOM出現。 描述具體的用例(可能在單獨的問題中),有人可以爲您提供建議。

+0

你不應該捕捉錯誤? – 2011-06-01 04:40:05

+0

@ Dr.Dredel從http://java.sun.com/docs/books/jls/second_edition/html/exceptions.doc.html - 第11.5節「類錯誤及其子類是普通程序通常不是普通程序的例外預計恢復'。所以是的 - 在大多數情況下,即使技術上可行,你也不應該犯錯誤。 OOM錯誤是最糟糕的之一,因爲它使系統處於不可預測的狀態。 – 2011-06-01 04:54:58

0

您可以使用一個未知的異常處理程序,當你的應用程序崩潰時的通知,從那裏,你可以重新載入您的活動。 但它非常重要的你確保這種情況發生的次數只有你看到一隻獨角獸的次數。最好你解決問題而不是解決問題。

這裏是一個鏈接,這將有助於UEH:How do I obtain crash-data from my Android application?

0

我認爲這個問題是從服務器上加載圖像,其增加導致應用程序在OutOfMemmory錯誤的記憶。 你必須避免你的應用程序佔用這麼大的內存。 YOu可以使用LazyLoading Concept加載圖像。

看一看用於sample program here

或看看here also

public class DrawableManager { 
    private final Map<String, Drawable> drawableMap; 

    public DrawableManager() { 
     drawableMap = new HashMap<String, Drawable>(); 
    } 

    public Drawable fetchDrawable(String urlString) { 
     if (drawableMap.containsKey(urlString)) { 
      return drawableMap.get(urlString); 
     } 

     Log.d(this.getClass().getSimpleName(), "image url:" + urlString); 
     try { 
      InputStream is = fetch(urlString); 
      Drawable drawable = Drawable.createFromStream(is, "src"); 
      drawableMap.put(urlString, drawable); 
      Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", " 
        + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", " 
        + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth()); 
      return drawable; 
     } catch (MalformedURLException e) { 
      Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e); 
      return null; 
     } catch (IOException e) { 
      Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e); 
      return null; 
     } 
    } 

    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) { 
     if (drawableMap.containsKey(urlString)) { 
      imageView.setImageDrawable(drawableMap.get(urlString)); 
     } 

     final Handler handler = new Handler() { 
      @Override 
      public void handleMessage(Message message) { 
       imageView.setImageDrawable((Drawable) message.obj); 
      } 
     }; 

     Thread thread = new Thread() { 
      @Override 
      public void run() { 
       //TODO : set imageView to a "pending" image 
       Drawable drawable = fetchDrawable(urlString); 
       Message message = handler.obtainMessage(1, drawable); 
       handler.sendMessage(message); 
      } 
     }; 
     thread.start(); 
    } 

    private InputStream fetch(String urlString) throws MalformedURLException, IOException { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(urlString); 
     HttpResponse response = httpClient.execute(request); 
     return response.getEntity().getContent(); 
    } 

} 

感謝 迪帕克

0
Intent intent = getIntent(); 
overridePendingTransition(0, 0); 
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
finish(); 
overridePendingTransition(0, 0); 
startActivity(intent); 

這將刷新或重新啓動活動。