简体   繁体   English

android 工作室中的图像视图显示在 xml 布局中,但未显示在应用程序中

[英]Image view in android studio is showing in xml layout but not showing in app

Image view in android studio is showing in xml layout but it is not showing in the app after run I tried every solution available but not got the solution to my problem. android 工作室中的图像视图以 xml 布局显示,但在运行后未显示在应用程序中我尝试了所有可用的解决方案,但没有找到解决我的问题的方法。

I'm making a simple meme share app in which there will be two buttons share and next, after clicking next button another meme appears I used an random meme api I have not completed the app ( not added function for next button and share button ) just to check whether meme image is showing or not I ran my code successfully it was installed on my phone, buttons are showing but image is not showing only white background我正在制作一个简单的模因共享应用程序,其中将有两个共享按钮,然后单击下一步按钮后出现另一个模因我使用了随机模因 api 我尚未完成该应用程序(未为下一个按钮和共享按钮添加 function )只是为了检查 meme 图像是否显示,我成功地运行了我的代码,它已安装在我的手机上,按钮正在显示,但图像不只显示白色背景

MainActivity.kt主活动.kt

package com.example.memeshare

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    loadMeme()
}

private fun loadMeme(){

  var memeImageview = findViewById<ImageView>(R.id.imageView)
// ...

// Instantiate the RequestQueue.
    val queue = Volley.newRequestQueue(this)
    val url = "https://i.redd.it/bi9tnlxmqlr71.jpg"


    val jsonObjectRequest = JsonObjectRequest(
        Request.Method.GET, url,null,
        { response ->

          val url = response.getString("url")
            Glide.with(this).load(url).into(memeImageview)
        },
        {  },
    )

// Add the request to the RequestQueue.
    queue.add(jsonObjectRequest)
}
fun shareMeme(view: View) {

}

fun nextMeme(view: View) {

}
}

activity_main.xml activity_main.xml

<?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=".MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:srcCompat="@tools:sample/avatars" />

<Button
    android:id="@+id/shareButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/share"
    android:padding="32dp"
    app:layout_constraintRight_toRightOf="@id/guideline2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:onClick="shareMeme"/>



<Button
    android:id="@+id/nextButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:width="0dp"
    android:padding="32dp"
    android:text="@string/next"
    app:layout_constraintLeft_toLeftOf="@id/guideline2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:onClick="nextMeme"/>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintGuide_percent="0.5"/>




</androidx.constraintlayout.widget.ConstraintLayout>

[It look like this in layout] [布局看起来像这样]

( https://i.stack.imgur.com/CarzW.jpg ) ( https://i.stack.imgur.com/CarzW.jpg )

You need to set the你需要设置

android:layout_width="wrap_content"
android:layout_height="wrap_content

And you need to add a source or background to the imageview.并且你需要给imageview加一个来源或者背景。

android:background="@drawable/image"
android:src="@drawable/image"

Your mistake is to use tools:srcCompat.你的错误是使用 tools:srcCompat。 So when you use tools, nothing will show in your app You have to use android:src="drawable/avatar"所以当你使用工具时,你的应用程序中不会显示任何内容你必须使用 android:src="drawable/avatar"

Thanks to everyone who answered my question after a lot of struggle to find why my image view is not loading I got the mistake, actually what I did wrong was in感谢所有在努力寻找为什么我的图像视图未加载后回答我问题的人我弄错了,实际上我做错了

val url = "https://i.redd.it/bi9tnlxmqlr71.jpg"

I put the image link in val url instead of API link which is "https://meme-api.herokuapp.com/gimme"我把图片链接放在val url而不是 API 链接,它是“https://meme-api.herokuapp.com/gimme”

So the correct one is:所以正确的是:

val url ="https://meme-api.herokuapp.com/gimme"

After this correction, my meme is loading.更正后,我的模因正在加载。

Also, "tools:srcCompat="@tools:sample/avatars" is only used for debugging, it doesn't do anything when you run the app.此外, "tools:srcCompat="@tools:sample/avatars"仅用于调试,在您运行应用程序时它不会执行任何操作。

Correct code,正确的代码,

MainActivity.kt主活动.kt

package com.example.memeshare
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        loadMeme()
    }

   private fun loadMeme(){

        var memeImageview = findViewById<ImageView>(R.id.imageView)
// ...

// Instantiate the RequestQueue.
        val queue = Volley.newRequestQueue(this)
        val url = "https://meme-api.herokuapp.com/gimme"

// Request a string response from the provided URL.
       val jsonObjectRequest = JsonObjectRequest(
               Request.Method.GET, url,null,
               {
                   response ->
                   val url = response.getString("url")
                   Glide.with(this).load(url).into(memeImageview)
               },
               {
                 Toast.makeText(this,"Something went wrong",Toast.LENGTH_LONG).show()
               }
               )


       queue.add(jsonObjectRequest)



   }

    fun nextMeme(view: View) {
        loadMeme()
    }

    fun shareMeme(view: View) {}


// Add the request to the RequestQueue.

    }

activity_main.xml activity_main.xml

    <?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=".MainActivity">

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

    
    <Button
        android:id="@+id/shareButton"
        android:layout_width="190dp"
        android:layout_height="wrap_content"
        android:text="@string/share"
        android:padding="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:onClick="shareMeme"/>



    <Button
        android:id="@+id/nextButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:width="190dp"
        android:padding="32dp"
        android:text="@string/next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:onClick="nextMeme"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintGuide_percent="0.5"/>




</androidx.constraintlayout.widget.ConstraintLayout>










 
     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM