2012-10-06 27 views
2

我剛剛向Android市場發佈了我的應用程序的新版本,而我的新版本在活動中有一個GLSurfaceView。儘管我沒有做任何事情,但我有一個龐大的用戶羣,那裏有很多不合標準的Android手機,而且我總是在GLThread.run()中獲得例外。處理GLThread.run()中的錯誤

什麼是推薦的方式來捕獲/處理這些異常而不會崩潰整個應用程序?理想情況下,我希望能夠捕獲錯誤,從活動中移除表面視圖並關閉使用OpenGL的組件。我做了一些搜索,但大部分都是在Android上發現Firefox的異常報告以及類似的東西。 :)

我想只使用未捕獲的異常處理程序,切換共享首選項標誌爲false,並讓它崩潰;下一次運行我不會嘗試添加GLSurfaceView。

回答

2

我最終解決該問題與下面的代碼:

final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); 

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { 
    @Override 
    public void uncaughtException(Thread thread, Throwable ex) { 
     if (thread.getName().startsWith("GLThread")) { 
      disableOpenGLStuff(); 
     } 

     // You could wrap this in an else, but I'm not sure how good of an idea it is to leave the application running when a thread has crashed. 
     defaultHandler.uncaughtException(thread, ex); 
    });