W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)

來源:互聯網
上載者:User



今天編程時候遇到一個問題:   W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)  。。。。
log完全沒有,,,這個時候有心殺賊,無力回天啊,,,,什麼報錯資訊都沒有。

百度各種問題解決方案,最終有一篇文章:http://blog.csdn.net/liqiangeastsun/article/details/43986605  講了一個問題的解決方案:
以下是原文:

Android錯誤threadid=1: thread exiting with uncaught exception (group=0x416298c8)

在項目開發中測試時崩潰,錯誤如上 
該錯誤的意思是線程中存在沒有捕獲到的異常。一般情況下捕獲異常使用

 try  { }  catch (Exception e) { }
1 2 3 4 5 6 1 2 3 4 5 6

但是線上程池中,線程池在執行任務時捕獲了所有異常,這樣一來線程中所有的異常都無法捕獲到拋出的異常。 
即 try catch 捕獲不到異常了。 
Java中有一個介面,UncaughtExceptionHandler 描述如下:

static interface Thread.UncaughtExceptionHandler // 當 Thread 因未捕獲的異常而突然終止時,調用處理常式的介面。
1 2 1 2

Thread類中的一個方法

static voidsetDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) //設定當線程由於未捕獲到異常而突然終止,並且沒有為該線程定義其他處理常式時所調用的預設處理常式。
1 2 3 1 2 3

我們需要實現這樣一個介面UncaughtExceptionHandler,然後在程式的主線程中設定處理常式。 
代碼如下

import java.lang.Thread.UncaughtExceptionHandler;//implements UncaughtExceptionHandlerpublic class Other extends Activity implements UncaughtExceptionHandler {//必須實現介面uncaughtException@Overridepublic void uncaughtException(Thread arg0, Throwable arg1) {        //在此處理異常, arg1即為捕獲到的異常          Log.i("AAA", "uncaughtException   " + arg1);    }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

現在還無法捕獲到線程中的異常,還需要調用一個方法

Thread.setDefaultUncaughtExceptionHandler(this);
1 1

在OnCreate方法中調用一下即可

@Overrideprotected void onCreate(Bundle savedInstanceState) {    //在OnCreate方法中調用下面方法,然後再使用線程,就能在uncaughtException方法中捕獲到異常    Thread.setDefaultUncaughtExceptionHandler(this);}
1 2 3 4 5 1 2 3 4 5

完整代碼如下

    //匯入包    import java.lang.Thread.UncaughtExceptionHandler;    //implements UncaughtExceptionHandler    public class Other extends Activity implements UncaughtExceptionHandler       {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.th);        //在此調用下面方法,才能捕獲到線程中的異常        Thread.setDefaultUncaughtExceptionHandler(this);        //擷取建立的 Button        btn1 = (Button) findViewById(R.id.btn);        //給btn1添加一個點擊監聽事件        btn1.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                //調用我自己的方法                Init();            }        });    }     public void Init() {        //開啟線程        new Handler(Looper.getMainLooper()).post(new Runnable() {               @Override            public void run() {                //此處的 try catch 是無法捕獲到異常的                try {                    //////////////////////////////////////////                    //需要線上程中調用的方法purchase.init(context, iapListener)                    purchase.init(context, iapListener);                    //////////////////////////////////////////                } catch (Exception e) {                    // TODO: handle exception                    Log.i("AAA", "INIG   " + e);                    e.printStackTrace();                }            }        });    }    //必須實現介面uncaughtException    @Override    public void uncaughtException(Thread arg0, Throwable arg1) {        //在此處理異常, arg1即為捕獲到的異常        Log.i("AAA", "uncaughtException   " + arg1);    }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

這樣再次運行就會捕獲到線程中的異常,不會導致項目直接崩潰


最終可以列印出log資訊了,報錯為:(Android5.0以上沒這個問題,5,0以下有問題的)     java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 
問題已經很明顯了,類強轉時候報的問題:
    解決方案: 

android.view.ViewGroup.LayoutParams
改為:
android.widget.AbsListView.LayoutParams



這個問題到不是很關鍵,主要問題是找到了方法,當log不能顯示異常資訊時候,,,,可以幫我們列印出log資訊,然後分析問題,解決問題。。。。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.