java.lang.StackOverflowError如何解决

 一,android布局太深导致的 java.lang.StackOverflowError

E/AndroidRuntime( 1900): java.lang.StackOverflowError 
E/AndroidRuntime( 1900):     at android.graphics.Canvas.drawText(Canvas.java:1269) 
E/AndroidRuntime( 1900):     at android.text.Layout.draw(Layout.java:337) 
E/AndroidRuntime( 1900):     at android.widget.TextView.onDraw(TextView.java:4006) 
E/AndroidRuntime( 1900):     at android.view.View.draw(View.java:6057) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1545) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.View.draw(View.java:6060) 
E/AndroidRuntime( 1900):     at android.view.View.buildDrawingCache(View.java:5823) 
E/AndroidRuntime( 1900):     at android.view.View.getDrawingCache(View.java:5661) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1481) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.widget.AbsListView.dispatchDraw(AbsListView.java:1322) 
E/AndroidRuntime( 1900):     at android.widget.ListView.dispatchDraw(ListView.java:2841) 
E/AndroidRuntime( 1900):     at android.view.View.draw(View.java:6164) 
E/AndroidRuntime( 1900):     at android.widget.AbsListView.draw(AbsListView.java:2169) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1545) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.View.draw(View.java:6060) 
E/AndroidRuntime( 1900):     at android.widget.FrameLayout.draw(FrameLayout.java:352) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1545) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.drawChild(ViewGroup.java:1543) 
E/AndroidRuntime( 1900):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1281) 
E/AndroidRuntime( 1900):     at android.view.View.draw(View.java:6060) 
E/AndroidRuntime( 1900):     at android.widget.FrameLayout.draw(FrameLayout.java:352) 
E/AndroidRuntime( 1900):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2038) 
E/AndroidRuntime( 1900):     at android.view.ViewRoot.draw(ViewRoot.java:1233) 
E/AndroidRuntime( 1900):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1042) 
E/AndroidRuntime( 1900):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1502) 
E/AndroidRuntime( 1900):     at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime( 1900):     at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime( 1900):     at android.app.ActivityThread.main(ActivityThread.java:3972) 
E/AndroidRuntime( 1900):     at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime( 1900):     at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime( 1900):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
E/AndroidRuntime( 1900):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:543) 
E/AndroidRuntime( 1900):     at dalvik.system.NativeStart.main(Native Method) 
I/Process (  833): Sending signal. PID: 1900 SIG: 3

 

解决方式:减少布局嵌套层数,特别是用到类似tabHost,ViewPager的控件时,要考虑到最底层的 activity 的

contentView的布局,尽量简化,多用RelativeLayout替代LinearLayout

同样再优化列表滑动效率的时候,简化item布局也能带来巨大的体验提升


二,函数调用层级太多导致的 java.lang.StackOverflowError,如死循环,递归等

StackOverflowError是由于当前线程的栈满了  ,也就是函数调用层级过多导致。

比如死递归。

如:

  public String homePage(){
  this.findAllNews();
   return "shouye";
 }

 

public String findAllNews(){
   List newslist= hotnewsService.findAll();
   HotnewsDto  hotnews= (HotnewsDto) newslist.get(0);//得到第一条新闻
   String content=hotnews.getContent();
   String subcontent=content.substring(0, 80);//截取这条新闻信息内容在首页部分显示
   super.setRequestAttribute("hotnews", hotnews);
   super.setRequestAttribute("newslist", newslist);
   super.setRequestAttribute("subcontent", subcontent);
   return homePage;

   }

  如红色标记处,这样子调用的方法,相当于一个无限循环了!

解决办法:

  我习惯将 findAllNews方法的返回值设为 void 即可





你可能感兴趣的:(java.lang.StackOverflowError如何解决)