1. 程式人生 > >StackOverflowError出現的原因及解決辦法

StackOverflowError出現的原因及解決辦法

06-12 10:28:31.750: E/AndroidRuntime(13995): FATAL EXCEPTION: main

06-12 10:28:31.750: E/AndroidRuntime(13995): java.lang.StackOverflowError

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.text.Layout.getLineForOffset(Layout.java:1059)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.text.Layout$Ellipsizer.getChars(Layout.java:2270)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.text.TextUtils.getChars(TextUtils.java:70)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.graphics.Canvas.drawText(Canvas.java:1328)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.text.Layout.draw(Layout.java:651)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.widget.TextView.onDraw(TextView.java:4326)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.View.draw(View.java:7094)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.drawChild(ViewGroup.java:1732)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.View.draw(View.java:6988)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.drawChild(ViewGroup.java:1732)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.View.draw(View.java:6988)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.drawChild(ViewGroup.java:1732)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.drawChild(ViewGroup.java:1730)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.drawChild(ViewGroup.java:1730)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.drawChild(ViewGroup.java:1730)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)

06-12 10:28:31.750: E/AndroidRuntime(13995):          at android.view.View.draw(View.java:6988)

從前面幾行分析,跟文字控制元件有關。最後問題定位結果就是因為listView中textVIew巢狀太深所至。 

         我的listVIew佈局層是這樣的: 

         Listview--->RelativeLayout--->RelativeLayout--->RelativeLayout--->LinearLayout--->TextView

         改為:

         Listview--->RelativeLayout--->RelativeLayout--->RelativeLayout --->TextView

總結:儘可能的使用簡單的佈局和檢視

      如果一個視窗包含很多的檢視,那麼啟動時間長、測量時間長、繪製時間長、佈局時間長;如果檢視樹深度太深,會導致StackOverflowException異常,和使用者介面反映會很慢很慢,如果在ListView 或者 GridView中使用則會更加嚴重。因此建議各位使用佈局的時候,一定要選擇合理的佈局,複雜的堆砌即使解決了我們對複雜佈局的處理,卻很有可能降低我們程式的閱讀效率,甚至丟擲意想不到的異常。

    解決的方法:

         1.使用TextView的複合drawables,減少層次(該方法我們開發過程中屢試不爽!!!)

         2.使用ViewStub延遲展開檢視

         3.使用<merge>合併檢視

         4.使用RelativeLayout減少層次

         5.自定義佈局

         以上見解純屬個人理解,歡迎大家多多指教,共同學習,共同進步!