2013-02-12 53 views
1

我得到一個計算器錯誤,當我在TextView的應用TransitionDrawable:的Android java.lang.StackOverflowError的TransitionDrawable

private static void applyAnimationTransitionBackground(String descriptorValue, final Resources resources, View view){ 
Drawable currentBackground; 
TransitionDrawable td; 

if (view!=null){ 

    //Get current background or transparent color 
    currentBackground = view.getBackground(); 
    if (currentBackground==null){ 
     currentBackground = new ColorDrawable(android.R.color.transparent); 
    } 

    //Create transition depend on trend 
    if (descriptorValue.equals("1")){ 
     td= new TransitionDrawable (new Drawable []{currentBackground, new ColorDrawable(resources.getColor(R.color.positive_animation))}); 
    }else if (descriptorValue.equals("2")){ 
     td= new TransitionDrawable (new Drawable []{currentBackground, new ColorDrawable(resources.getColor(R.color.neutral_animation))}); 
    }else{ 
     td= new TransitionDrawable (new Drawable []{currentBackground, new ColorDrawable(resources.getColor(R.color.negative_animation))}); 
    } 

    //Apply animation 
    if (td!=null){ 
     view.setBackgroundDrawable(td); 
     td.startTransition(500); 
     //td.reverseTransition(500);    
    }else{ 
     Logger.error("applyAnimationTransitionBackground: TransitionDrawable is NULL"); 
    } 

} 

}

02-12 17:00:12.899: I/System.out(11217): ---> M SAN nuevo valor 1,52% 
02-12 17:00:12.907: I/System.out(11217): ---> M SAN nuevo valor SANTANDER 
02-12 17:00:12.907: W/Resources(11217): Converting to string: TypedValue{t=0x11/d=0x55 a=-1} 
02-12 17:00:12.907: W/Resources(11217): Converting to string: TypedValue{t=0x10/d=0xfffffffe a=-1} 
02-12 17:00:12.915: I/System.out(11217): ---> M SAN nuevo valor 1,52% 
02-12 17:00:12.915: I/System.out(11217): ---> M SAN nuevo valor SANTANDER 
02-12 17:00:12.915: W/Resources(11217): Converting to string: TypedValue{t=0x11/d=0x55 a=-1} 
02-12 17:00:12.915: W/Resources(11217): Converting to string: TypedValue{t=0x10/d=0xfffffffe a=-1} 
02-12 17:00:12.915: I/dalvikvm(11217): threadid=1: stack overflow on call to Landroid/view/GLES20Canvas;.setupModifiers:IL 
02-12 17:00:12.915: I/dalvikvm(11217): method requires 40+20+20=80 bytes, fp is 0x4016b324 (36 left) 
02-12 17:00:12.915: I/dalvikvm(11217): expanding stack end (0x4016b300 to 0x4016b000) 
02-12 17:00:12.915: I/dalvikvm(11217): Shrank stack (to 0x4016b300, curFrame is 0x4016e420) 
02-12 17:00:12.915: D/AndroidRuntime(11217): Shutting down VM 
02-12 17:00:12.915: W/dalvikvm(11217): threadid=1: thread exiting with uncaught exception (group=0x40bd91f8) 
02-12 17:00:12.961: E/AndroidRuntime(11217): FATAL EXCEPTION: main 
02-12 17:00:12.961: E/AndroidRuntime(11217): java.lang.StackOverflowError 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.view.GLES20Canvas.drawRect(GLES20Canvas.java:942) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.view.GLES20RecordingCanvas.drawRect(GLES20RecordingCanvas.java:226) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.view.GLES20Canvas.drawRect(GLES20Canvas.java:952) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.view.GLES20RecordingCanvas.drawRect(GLES20RecordingCanvas.java:232) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.ColorDrawable.draw(ColorDrawable.java:70) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 
02-12 17:00:12.961: E/AndroidRuntime(11217): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195) 

回答

0

我幾乎可以肯定你在一個循環中調用applyAnimationTransitionBackground(),例如onDrawFrame()的一個Renderer(我說這只是因爲我看到GL ES的使用,但它可能是某種或另一種事情的動畫)。

我這麼認爲是因爲你設置了背景view。在view的第一個背景是兩種顏色之間的轉換可繪製 - 我們稱之爲T(C1, C2)。對於每一個連續的呼叫中,view的背景設置爲之間的過渡可繪製:

  1. view當前背景和
  2. 一些顏色繪製

在效果:

  1. 後第二次電話,view背景設置爲轉換T(T(C1, C2), C3)
  2. 第三次通話後,view後臺設置爲轉換T(T(T(C1, C2), C3), C4)
  3. 等等...

實際上,即使經過100個電話,你有一個背景繪製設置爲100個過渡可繪製一個大規模組合。您可以想象在對您的方法進行1000或10000次調用後會發生什麼。由於轉換可繪製只是將繪圖委託給它們的組件繪圖,所以最終會出現堆棧溢出。

最簡單的方法來檢查是否正是這種情況是將行Logger.info("call");放在applyAnimationTransitionBackground()的開頭。如果在日誌(LogCat)中看到很多call消息,則情況就是如此。

要解決這個問題,你只需要從這種組合drawable的模式中辭職。例如,您可以保存用於轉場的最後一個顏色值及其開始時間。然後,當您需要在當前顏色和新顏色之間創建過渡時,可以使用保存的開始時間和顏色來內插背景的當前顏色。然後,您可以簡單地創建一個新的轉換可繪製 - 在當前顏色和新顏色之間。

相關問題