Android兩種常見錯誤(ANR和FC)解決辦法

ANR(Activity Not Respone)(無響應)html

    先介紹下Main線程(也稱爲UI線程、主線程)web

    功能: 1.建立UI控件json

             2.更新UI控件狀態網絡

             3.事件處理post

    限制:Main線程不建議有超過5秒的事件ui

    出現條件:spa

        當用戶輸入事件5s內沒有獲得響應,將彈出ANR對話框線程

        廣播接收者的onReceive()執行時間超過10s日誌

    解決方案(原則):orm

        全部可能的耗時操做都要在子線程()中執行

        常見耗時操做:

            I/O:網絡操做

                   SDcard 

            數據運算


FC(Force close)

    緣由:

        1.Error

        OOM(out of memory error)

        StackOverFlowError

        2.RuntimeException

    解決辦法:

        看日誌


    子線程不能更新UI的解決思路:

       一、 將子線程執行結果發送到Main線程:handler+massage線程間通信

            發送消息:

                        Message msg = mHandler.obtainMessage(UPLOADjson);    

                        mHandler.sendMessage(msg);

            接收消息:

        public void handleMessage(Message msg) {

            switch (msg.what) {

            case UPLOAD:

                mTextView2.setText((String) msg.obj);

            break;

            case DOWNLOAD:

                mTextView.setText((String) msg.obj);

                break;

            default:

                break;

            }

        }


      二、 使更新ui代碼段在Main線程執行:

        1.runOnUiThread

        2.view.post view.postDelay()

        3.handler.post handler.postDelay()


參考:

http://www.cnblogs.com/mengdd/p/3418780.html

相關文章
相關標籤/搜索