2011-07-15 155 views
1

我開發了一個應用程序,其中我有4個選項卡A,B,C,D。每個選項卡都包含一個活動。在第四個選項卡D中,我添加了一個ActivityGroup,其中有3個活動X,Y,Z。在第四個Tab中,我添加了活動組,我得到了StackOverflow錯誤。請幫我解決這個錯誤。修復StackOverflow錯誤

這是我的堆棧跟蹤: -

07-15 15:22:45.587: ERROR/AndroidRuntime(222): Uncaught handler: thread main exiting due to uncaught exception 
07-15 15:22:45.677: ERROR/AndroidRuntime(222): java.lang.StackOverflowError 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.AbsListView.dispatchDraw(AbsListView.java:1324) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.ListView.dispatchDraw(ListView.java:2972) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6641) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.AbsListView.draw(AbsListView.java:2171) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.view.View.draw(View.java:6538) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
07-15 15:22:45.677: ERROR/AndroidRuntime(222):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow 

請幫我...我很新到Android。

+0

發佈您的代碼。這個堆棧跟蹤是不可讀的 –

+1

沒有看到導致錯誤的代碼,真的很難知道哪裏出了問題,但看起來你最終會在某處找到一個耗費內存的遞歸。 –

+0

發佈代碼而不是堆棧跟蹤 – Randroid

回答

1

我想你有一個無限循環的地方。

也許你已經在層次結構中的某處添加了兩次相同的視圖實例,現在你已經有了一個循環。

修復此問題。並修復你的佈局。我看不到有這樣一個複雜佈局的理由。

+0

嗨佩德羅loureiro!我認爲你非常正確地得到了我的錯誤。你可以告訴我如何跟蹤我使用同一個視圖實例的位置。還有一件事我想問一下,我們應該用哪個FLAG CONSTANTS與Intent一起用新視圖替換Tab的contentView? ?+ –

+0

絕對不會通過代碼添加視圖,除非您在那一刻創建了它並且不要添加兩次! (絕對不要findViewById,然後在其他地方添加返回的視圖)。我無法回答其他問題。 –

+0

無法添加視圖兩次,它立即拋出異常提及該視圖已附加。所以這不能成爲堆棧溢出的原因。 – 3c71

4

我同意在大多數情況下還有另一種佈局方式,但如果您真的需要處理大量的嵌套視圖,則存在骯髒的解決方法。在您的視圖子類:

private Handler mHandler = new Handler(); 

    @Override 
    public void draw(Canvas canvas) { 
      try { 
        super.draw(canvas); 
      } catch (StackOverflowError e) { 
        mHandler.postDelayed(new Runnable() { 
          public void run() { 
            invalidate(); 
          } 
        }, 1); 
      } 
    } 

它將停止的情況下,失敗的當前調用堆棧,延緩當前視圖的抽獎,在不久的將來。並不是說它只是通過調用invalidate()(不需要使用postDelayed)來工作,但我仍然更喜歡使用處理程序來創建延遲。

我仍然同意其他人最好的解決方案是做出更好的佈局。只有在沒有其他方式進行佈局時才應使用此解決方法。

編輯:這種解決方法似乎並沒有真正的工作。看來,解決問題的唯一方法是減少視圖層次結構的深度。

+4

這是我見過的第一個真正巧妙的想法,它解決了Android操作系統缺乏設計帶來的荒謬侷限。告訴人們重構佈局,因爲操作系統如此薄弱並不是合理的答案 – talkol

+0

我'這個解決方案有問題,因爲調用super.draw(canvas)失敗與StackOverflowError以某種方式觸發另一個未來調用draw()再次從主層次而不是處理程序,所以我得到一個無限循環..此代碼原因無限循環太: \t公共無效畫(油畫畫布) \t {\t \t \t \t嘗試 \t \t { \t \t \t super.draw(canvas); \t \t} \t \t趕上(的StackOverflowError E) \t \t { \t \t \t} \t \t } – talkol

+0

它增加的嵌套視圖的最大深度,但是,但它只是將不會得出一些的內視圖,但是當我通過層次結構視圖查看視圖時,我可以看到所有視圖正常 –

0

我搜索遍Google,但找不到解決方案。然後,我剛剛提出了這個想法,它完全適用於我的舊三星Galaxy Nexus和沃達豐VF695!

這是我在我很深的佈局根的FrameLayout黑客:

public class StackFrameLayout extends FrameLayout { 
    private boolean stackOverflow; 

    @Override 
    public void draw(final Canvas canvas) { 
     if (stackOverflow) { 
      stackDraw(canvas); 
     } else { 
      try { 
       super.draw(canvas); 
      } catch (StackOverflowError e) { 
       stackOverflow = true; 
       stackDraw(canvas); 
      } 
     } 
    } 

    public void stackDraw(final Canvas canvas) { 
     Thread thread = new Thread(getContext().getMainLooper().getThread().getThreadGroup(), new Runnable() { 
      @Override 
      public void run() { 
       StackFrameLayout.super.draw(canvas); 
      } 
     }, "draw-thread", 2000000); 
     thread.start(); 
     try { 
      thread.join(); 
     } catch (InterruptedException e1) { 
      e1.printStackTrace(); 
     } 
    } 
} 

所以基本上我通過複雜的繪圖方法成線具有較大堆棧大小。所以被稱爲方法的堆棧將停止增加這一點。然後,UI線程等待繪圖線程完成其工作,然後繼續確保按照在UI線程上運行的順序進行繪製。