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

标签 java android

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

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/

相关文章:

Java SQLException 与 JDBC - SQLITE_MISUSE

android - Android Native 模块与现有 Xamarin Native 项目的集成

android - Android 4.0 上出现异常 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`

android - 开发 Android 主屏幕

android - 将 ListView 中的选择从橙色更改为绿色

java - Android webview - 在后台保持 Activity 状态并在页面更新时创建通知?

java - 如何防止使用反射创建多个对象

java - 如何在 JAR\Java 项目中存储值

Android 简单 2 路 IM 问题(可运行)

java - Camel : stop the route when the jdbc connection loss is detected