我有一个Recyclerview。在它的适配器中,这就是我所拥有的:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
//gradient is my arraylist that contains resource IDs. these are set in my fragment class
int res = gradient.get(position);
holder.parent.setBackground(ContextCompat.getDrawable(Application.getAppContext(), res));
}
并且在我的片段中,我将这些资源ID传递给适配器:
final ArrayList<Integer> gradients = new ArrayList<>();
gradients.clear();
gradients.add(R.drawable.gradient);
gradients.add(R.drawable.gradientgray);
gradients.add(R.drawable.gradient2);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(gradients) //it has a constructor that gets the arraylist and sets it to the arraylist in adapter class.
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
[它可在android> 4.4上运行,但在ResourceRecovery Exception的android 4.4上崩溃。
编辑:堆栈跟踪:
FATAL EXCEPTION: main
Process: example.myapp, PID: 1652
android.content.res.Resources$NotFoundException: Resource ID #0x7f070074
at android.content.res.Resources.getValue(Resources.java:1123)
at android.content.res.Resources.getDrawable(Resources.java:698)
at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:456)
at example.myapp.ui.dashboard.DashboardRecycler.onBindViewHolder(DashboardRecycler.java:62)
at example.myapp.ui.dashboard.DashboardRecycler.onBindViewHolder(DashboardRecycler.java:22)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
一个可绘制对象:(它们全部相同,但颜色不同)
<?xml version="1.0" encoding="UTF-8"?>
<gradient
android:endColor="@android:color/holo_green_dark"
android:startColor="@android:color/holo_green_light"
android:type="linear" />
<corners
android:radius="0dp"/>
可绘制对象就像这样,但是它们的颜色不同。
[我也尝试在recyclerView的适配器中设置不带arraylist的drawable(例如R.drawable.gradient而不是arraylist.get(position)),并且工作正常。但是使用arraylist可以在更高的android版本上正常工作。我知道使缓存无效并不相关,但这也无济于事。