You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中XML显示ImageView但运行应用后图片不显示求助

问题原因&解决方案

1. 核心逻辑错误:请求类型与接口返回不匹配

你当前传入Volley的请求地址是直连图片的静态资源链接,返回内容为二进制图片数据,并非JSON格式,使用JsonObjectRequest发起请求会直接解析失败走错误回调,而你的错误回调为空实现,无法感知到报错,所以图片不会加载。
如果仅测试图片加载功能,无需通过Volley发起请求,直接用Glide加载图片地址即可,修改loadMeme代码如下:

private fun loadMeme(){
  val memeImageview = findViewById<ImageView>(R.id.imageView)
  val testImgUrl = "https://i.redd.it/bi9tnlxmqlr71.jpg"
  Glide.with(this).load(testImgUrl).into(memeImageview)
}

如果要对接随机梗图API,需要更换为返回JSON格式的接口地址,这类接口的返回体中才包含可提取的图片url字段。

2. 网络权限缺失

确认在AndroidManifest.xml中添加了网络访问权限,否则所有网络请求都会被系统拦截:
在manifest标签下(与application标签同级)添加如下代码:

<uses-permission android:name="android.permission.INTERNET" />

3. 布局适配优化

你当前的ImageView底部约束到父布局底部,会和下方的按钮区域重叠,导致图片底部被遮挡,可修改约束避免适配问题,同时添加缩放类型保证图片正常展示:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:scaleType="centerCrop"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toTopOf="@id/shareButton"
    tools:srcCompat="@tools:sample/avatars" />

4. 错误日志补充

后续开发过程中不要空实现网络请求的错误回调,添加日志打印可以快速定位问题:

val jsonObjectRequest = JsonObjectRequest(
    Request.Method.GET, url,null,
    { response ->
        val imgUrl = response.getString("url")
        Glide.with(this).load(imgUrl).into(memeImageview)
    },
    { error ->
        Log.e("LOAD_ERR", "图片加载请求失败:${error.message}")
    },
)

内容的提问来源于stack exchange,提问作者Divyang Tiwari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 07:54:02