繁体   English   中英

Android Studio:Resources.NotFoundException

[英]Android Studio: Resources.NotFoundException

我的代码在模拟器上运行良好,但是,当我尝试在设备(在本例中为 Nexus 5)上运行我的代码时,我开始得到很多 Resources.NotFoundExceptions。 大多数例外情况与可绘制文件或颜色文件有关。 例如:

android.content.res.Resources$NotFoundException: File res/drawable/blob_ocean.xml from drawable resource ID #0x7f060066

Logcat 然后指出错误是由以下原因引起的:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/blob_ocean.xml from drawable resource ID #0x7f060066

然后它指出先前的错误是由以下原因引起的:

 Caused by: android.content.res.Resources$NotFoundException: File res/color/radial_pink.xml from color state list resource ID #0x7f04004e

这两个例子出现在一个片段上,与 AnimatedVectorDrawable 一致。

public class BlobFragment extends Fragment {
private AnimatedVectorDrawable blob;
private ImageView blobView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.blob_fragment, container, false);
    blobView = (ImageView) v.findViewById(R.id.blob_o);
    blob = (AnimatedVectorDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.blob_ocean, null);
    blobView.setImageDrawable(blob);
    blob.start();
    return v;
}

}

片段的布局如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OceanThemeActivity">

    <ImageView
        android:id="@+id/blob_o"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:srcCompat="@drawable/blob_ocean"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.271"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我在网上查看过,但找不到有效的东西。 很难确定问题,因为在模拟器中一切正常。

我解决了这个问题。 它与向后兼容性有关; 我使用的是仅适用于 API 24 或更高版本的 AnimatedVectorDrawable,而我的设备较低。 将对象更改为 AnimatedVectorDrawableCompat 修复了它。 此外,一些可绘制的资源太大,使它们更小工作得很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM