2017-02-16 46 views
2

我想用一個PNG圖像中一個TextView的背景添加一些連鎖反應,發現this Post設置背景繪製到TextView的casues java.lang.StackOverflowError的

我action_darwer.xml繪製作爲

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="?attr/selectableItemBackgroundBorderless" android:state_pressed="true" /> <!-- pressed --> 
    <item android:drawable="@drawable/drawer" /> <!-- default --> 
</selector> 

並將其設置爲TextView的作爲

<TextView android:id="@+id/action_drawer" 
    android:layout_marginLeft="20dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/action_drawer"/> 

它不是在所有的工作,並給予錯誤

java.lang.StackOverflowError: stack size 8MB 
      at android.content.res.ColorStateList.valueOf(ColorStateList.java:102) 
      at android.graphics.drawable.RippleDrawable$RippleState.<init>(RippleDrawable.java:919) 
      at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:914) 
      at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:94) 
      at android.graphics.drawable.LayerDrawable.<init>(LayerDrawable.java:126) 
      at android.graphics.drawable.LayerDrawable.<init>(LayerDrawable.java:122) 
      at android.graphics.drawable.RippleDrawable.<init>(RippleDrawable.java:979) 
      at android.graphics.drawable.RippleDrawable.<init>(RippleDrawable.java:94) 
      at android.graphics.drawable.RippleDrawable$RippleState.newDrawable(RippleDrawable.java:945) 
      at android.content.res.Resources.getCachedDrawableLocked(Resources.java:2777) 
      at android.content.res.Resources.getCachedDrawable(Resources.java:2745) 
      at android.content.res.Resources.loadDrawable(Resources.java:2586) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
      at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:182) 
      at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127) 
      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1133) 
      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1037) 
      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2718) 
      at android.content.res.Resources.loadDrawable(Resources.java:2614) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
      at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:182) 
      at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127) 
      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1133) 
      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1037) 
      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2718) 
      at android.content.res.Resources.loadDrawable(Resources.java:2614) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
      at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:182) 
      at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127) 
      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1133) 
      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1037) 
      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2718) 
      at android.content.res.Resources.loadDrawable(Resources.java:2614) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
      at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:182) 
      at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127) 
      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1133) 
      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1037) 
      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2718) 
      at android.content.res.Resources.loadDrawable(Resources.java:2614) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
      at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:182) 
      at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127) 
      at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1133) 
      at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1037) 
      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2718) 
      at android.content.res.Resources.loadDrawable(Resources.java:2614) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
      at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:182) 
      at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127) 
     at android.graphi 
02-16 13:12:53.567 22641-22641/org.team.asl.carticon E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! 
02-16 13:12:53.568 22641-22641/org.team.asl.carticon E/AndroidRuntime: Error reporting crash 
     android.os.TransactionTooLargeException 
      at android.os.BinderProxy.transactNative(Native Method) 
      at android.os.BinderProxy.transact(Binder.java:504) 
      at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4294) 
      at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89) 
      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) 
      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 

如何解決這個問題?任何建議將不勝感激。謝謝。

+0

顯示'抽拉/ drawer'代碼,或它是一個圖像? –

+0

@VladMatvienko是的,這是一個圖像! –

+0

你確定嗎?你能發佈資源文件夾結構的截圖嗎? –

回答

0

至於我本人而言, 不能定義 ?ATTR/selectableItemBackgroundBorderless作爲繪製,多數民衆贊成在飛機墜毀。 更改爲其他繪製

如:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/pressed" android:state_pressed="true" /> <!-- pressed --> 
    <item android:drawable="@drawable/drawer" /> <!-- default --> 
</selector> 
1

的StackOverflowError是因爲應用程序遞歸太深。

在第一修改background

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:state_pressed="true" 
     android:drawable="@drawable/pressed/> // Press State 
<item android:state_focused="true" 
     android:drawable="@drawable/focused"/> // Focused 
<item android:drawable="@drawable/drawer"/> // Default State 

</selector> 

詳情檢查How can I modify ripple color when using ?

+0

我需要背景中的漣漪效應,所以任何drawable.xml文件即按下狀態 –

+0

@ArshadAli http://stackoverflow.com/questions/27579626/set-background-on-textview-object-and-maintain-ripple-effect –

+0

@ArshadAli FYI另一個信息,'drawable'不是很大的尺寸/分辨率 –