由于矢量可绘制对象,KitKat 出现 StackOverflowError
StackOverflowError on KitKat because of vector drawables
我有一个具有以下视图层次结构的自定义视图:
[LinearLayout]
[CardView]
[LinearLayout]
[RecyclerView]
最后一个 RecyclerView
的每一项都包含 3 个矢量可绘制对象。当我 运行 KitKat (api 19) 上的应用程序时,WhosebugError
发生
FATAL EXCEPTION: main
Process: com.tsse.spain.myvodafone, PID: 3405
java.lang.WhosebugError
at android.graphics.Matrix.mapVectors(Matrix.java:684)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.getMatrixScale(VectorDrawableCompat.java:1300)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.drawPath(VectorDrawableCompat.java:1207)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.drawGroupTree(VectorDrawableCompat.java:1184)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.draw(VectorDrawableCompat.java:1193)
at android.support.graphics.drawable.VectorDrawableCompat$VectorDrawableCompatState.updateCachedBitmap(VectorDrawableCompat.java:1023)
at android.support.graphics.drawable.VectorDrawableCompat.draw(VectorDrawableCompat.java:366)
at android.view.View.draw(View.java:14450)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4477)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3869)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4477)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940
我正在 Application
class 的 onCreate()
中使用 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
和 build.gradle
[= 中的 vectorDrawables.useSupportLibrary = true
启用矢量绘图19=]
当我使用普通 PNG 绘图时,错误不会发生。
我做错了什么?
在 onBindViewHolder
中以编程方式创建您的 vector-drawable 并将其设置为您的视图,如下所示:-
Drawable your_vector_drawable = VectorDrawableCompat.create(context.getResources(), R.drawable.vectorDrawable, null);
现在将此可绘制对象设置为您在 onBindViewHolder
中的视图...
yourView.setBackground(your_vector_drawable);
这有望解决:)
This is a lower api issue...
我解决了这个问题,方法是先将矢量可绘制对象转换为 Bitmap
,然后使用 ImageView.setImageBitmap(Bitmap)
将其提供给 ImageView。这性能较低,但可以完成工作
我有一个具有以下视图层次结构的自定义视图:
[LinearLayout]
[CardView]
[LinearLayout]
[RecyclerView]
最后一个 RecyclerView
的每一项都包含 3 个矢量可绘制对象。当我 运行 KitKat (api 19) 上的应用程序时,WhosebugError
发生
FATAL EXCEPTION: main
Process: com.tsse.spain.myvodafone, PID: 3405
java.lang.WhosebugError
at android.graphics.Matrix.mapVectors(Matrix.java:684)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.getMatrixScale(VectorDrawableCompat.java:1300)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.drawPath(VectorDrawableCompat.java:1207)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.drawGroupTree(VectorDrawableCompat.java:1184)
at android.support.graphics.drawable.VectorDrawableCompat$VPathRenderer.draw(VectorDrawableCompat.java:1193)
at android.support.graphics.drawable.VectorDrawableCompat$VectorDrawableCompatState.updateCachedBitmap(VectorDrawableCompat.java:1023)
at android.support.graphics.drawable.VectorDrawableCompat.draw(VectorDrawableCompat.java:366)
at android.view.View.draw(View.java:14450)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4477)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3869)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4477)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940
我正在 Application
class 的 onCreate()
中使用 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
和 build.gradle
[= 中的 vectorDrawables.useSupportLibrary = true
启用矢量绘图19=]
当我使用普通 PNG 绘图时,错误不会发生。
我做错了什么?
在 onBindViewHolder
中以编程方式创建您的 vector-drawable 并将其设置为您的视图,如下所示:-
Drawable your_vector_drawable = VectorDrawableCompat.create(context.getResources(), R.drawable.vectorDrawable, null);
现在将此可绘制对象设置为您在 onBindViewHolder
中的视图...
yourView.setBackground(your_vector_drawable);
这有望解决:)
This is a lower api issue...
我解决了这个问题,方法是先将矢量可绘制对象转换为 Bitmap
,然后使用 ImageView.setImageBitmap(Bitmap)
将其提供给 ImageView。这性能较低,但可以完成工作