ImageView问题,图片不显示或抛出错误

ImageView problems, image not displaying or throwing error

我目前正在尝试在我的应用程序的登录屏幕上放置背景图片。我无法显示图像。

尝试的第一种方法:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/login_bg"
    android:scaleType="centerCrop"
    android:contentDescription="@string/description_bg" />

这会打开应用程序,但不会显示图像。 (它显示的是白色背景)。

尝试了第二种方法:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/login_bg"
    android:scaleType="centerCrop"
    android:contentDescription="@string/description_bg" />

其中returns错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: package, PID: 30016
              java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.LoginActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.widget.ImageView

完整堆栈跟踪 here

我正在我的 LoginActivity 中扩展 Activity

编辑

我刚刚尝试了另一张图片,效果很好。为什么 this 特定图像不起作用?

我的文件夹结构:

我觉得你的XML没有问题:

android:src="@drawable/login_bg"

问题可能出在你的图片上login_bg

在这个地方尝试使用不同的图像 login_bg,如果有效,那么您的图像可能有问题,因此请尝试使用不同的图像。

android:src="@drawable/new_image"

更新答案

所以,在检查您编辑的问题后:

我认为您在您的应用中使用了高分辨率图像,它有一个 OutOfMemoryError 日志文件中的错误。

所以您应该在您的清单中定义 android:largeHeapandroid:hardwareAccelerated,如下所示:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:hardwareAccelerated="false"
android:theme="@style/AppTheme"
....
....

希望我的回答对您有所帮助。