gpt4 book ai didi

java - 尝试使用 setImageResource 时应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:44 26 4
gpt4 key购买 nike

我正在构建另一个更大的应用程序,在某些时候我需要动态更改图像,并且我的应用程序崩溃了。因此,为了测试功能,我制作了另一个非常简单的应用程序,它只显示带有代码的图片:

package com.example.imagetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageResource(R.drawable.pugs);
}
}

The error reported by Android Studio is: 03-07 11:30:01.146 20758-20758/? E/Trace: error opening trace file: No such file or directory (2) 03-07 11:30:01.284 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper', referenced from method android.support.v4.view.ViewCompat.addOnUnhandledKeyEventListener 03-07 11:30:01.286 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.dispatchApplyWindowInsets 03-07 11:30:01.298 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.onApplyWindowInsets 03-07 11:30:01.300 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method android.support.v4.view.ViewCompat.removeOnUnhandledKeyEventListener 03-07 11:30:01.306 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$1', referenced from method android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener 03-07 11:30:01.354 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 03-07 11:30:01.396 20758-20758/com.example.imagetest E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagetest/com.example.imagetest.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID

0x7f060063

还有很多其他的东西。这是奇怪的事情:我将图片更改为另一个文件夹“mipmap”(在它位于可绘制之前:res/drawable)并且应用程序没有崩溃!但它也不显示图像,屏幕上什么也没有。 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">

<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

不确定图像是否重要,但这是一张非常可爱的 3 只哈巴狗图片(分辨率约为 4k),我只需通过复制/粘贴将其添加到可绘制对象中。

最佳答案

您的问题是资源(图像)的位置错误。您使用的设备的 API 低于 24,因此 API 23 及以下的资源为空,因为它们仅 API 24 及更高版本。

请将您的哈巴狗图片放在 drawable 文件夹中,而不是 drawable-v24 文件夹中,并且不要使用巨大的图像!

关于java - 尝试使用 setImageResource 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56420683/

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