Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

java - Android Studio:Resources.NotFoundException(Android Studio: Resources.NotFoundException)

My code ran fine on the emulator, however, as soon as I tried running my code on a device (in this case a Nexus 5) I started to get A LOT of Resources.NotFoundExceptions.

(我的代码在模拟器上运行良好,但是,当我尝试在设备(在本例中为Nexus 5)上运行代码时,我开始获得了大量Resources.NotFoundExceptions。)

The majority of the exceptions occur in relation to drawable or color files.

(大多数例外情况与可绘制文件或颜色文件有关。)

For example:

(例如:)

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

Logcat then states that the error is caused by:

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

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

then it states that the previous error is caused by:

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

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

The two examples occur in a fragment on the line with the AnimatedVectorDrawable.

(这两个示例出现在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;
}

}

(})

The layout for the fragment looks like this:

(片段的布局如下所示:)

    <?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>

I have looked online but have not been able to find something that works.

(我在网上看过,但找不到有效的方法。)

Having a hard time identifying the issue as everything is working fine in an emulator.

(由于在模拟器中一切正常,因此很难识别问题。)

  ask by Kübra Uzun translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved the issue.

(我解决了这个问题。)

It was related to backward compatibility;

(它与向后兼容性有关。)

I was using AnimatedVectorDrawable which is only working for API 24 or higher and my device was lower.

(我使用的AnimatedVectorDrawable仅适用于API 24或更高版本,而我的设备更低。)

Changing the objects to AnimatedVectorDrawableCompat fixed it.

(将对象更改为AnimatedVectorDrawableCompat可以修复它。)

Also, some of the drawable recourses were too big, making them smaller worked perfectly fine.

(另外,一些可绘制的资源太大,使它们变小就可以了。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...