gpt4 book ai didi

android - ResourceNotFoundException android kitkat

转载 作者:行者123 更新时间:2023-12-02 07:30:57 25 4
gpt4 key购买 nike

我有一个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上运行,但是在ResourceReFound 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版本上正常工作。
我知道使缓存无效并不相关,但这也无济于事。

最佳答案

我想通了!
一些可绘制对象就像“gradient2.xml(mdpi)”,所以我删除了它们并为它们创建了一个新的xml文件。
发生这种情况是因为我正在复制粘贴xml文件以创建新的xml文件。

关于android - ResourceNotFoundException android kitkat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61321499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com