Android 如何更改图像?
我试图在单击按钮时更改图像,但它不起作用。谁能告诉我怎么解决这个问题吗?我认为这与声明有关Android 如何更改图像?,android,kotlin,Android,Kotlin,我试图在单击按钮时更改图像,但它不起作用。谁能告诉我怎么解决这个问题吗?我认为这与声明有关 val afterEatingImage:ImageView=“R.drawable.after\u cookie” 我试着=(R.drawable.after\u cookie),R.drawable.after\u cookie,after\u cookie 也许我不应该使用Val作为图像,但我不知道该使用什么。所以你可以看到这是一个基本的问题,我是一个初学者,所以请帮助 fun eating(
val afterEatingImage:ImageView=“R.drawable.after\u cookie”
我试着=(R.drawable.after\u cookie),R.drawable.after\u cookie,after\u cookie
也许我不应该使用Val作为图像,但我不知道该使用什么。所以你可以看到这是一个基本的问题,我是一个初学者,所以请帮助
fun eating(view: View?) {
val afterEatingText:String="I am so full!!"
displayMessage(afterEatingText)
val afterEatingImage:ImageView="R.drawable.after_cookie"
displayImage(afterEatingImage)
}
private fun displayMessage(message: String) {
val afterEatingText = findViewById<View>(R.id.before_eating) as TextView
afterEatingText.text = message
}
private fun displayImage(image: ImageView){
val afterEatingImage: findViewById<View>(R.id.before_cookie) as ImageView
afterEatingImage.setImageResource(image)
}
}
如果我使用val textView=findViewById(进食前的R.id)
然后出现红色警报。
如果我使用val textView=findViewById(R.id.before_eating)作为textView
就可以了,为什么?
您要做的是将字符串R.drawable.after\u cookie分配给您的ImageView afterEatingImage。将此ImageView传递给方法,并尝试将其指定为图像资源 你应该做的是以下几点
fun eating(view: View?) {
val afterEatingText:String="I am so full!!"
displayMessage(afterEatingText)
displayImage(R.drawable.after_cookie) //Change this
}
private fun displayMessage(message: String) {
val afterEatingText = findViewById<View>(R.id.before_eating) as TextView
afterEatingText.text = message
}
private fun displayImage(imageResource: Int) { //Change this
val afterEatingImage: ImageView = findViewById<ImageView>(R.id.before_cookie)
afterEatingImage.setImageResource(imageResource)
}
趣味饮食(视图:视图?){
val afterEatingText:String=“我太饱了!!”
显示消息(afterEatingText)
displayImage(R.drawable.after\u cookie)//更改此设置
}
私人娱乐显示消息(消息:字符串){
val afterEatingText=findViewById(R.id.before_eating)作为文本视图
afterEatingText.text=消息
}
private fun displayImage(imageResource:Int){//更改此设置
val afterEatingImage:ImageView=findViewById(R.id.before\u cookie)
afterEatingImage.setImageResource(imageResource)
}
注意:我假设这些都是在您的活动中完成的。如果在片段中执行此操作,则需要视图的引用来查找ImageView
afterEatingImage.setImageResource=image
语法不正确,请将其替换为afterEatingImage.setImageResource(image)
感谢您的快速响应。我改变了这一点,但还有其他几个错误。我认为您在XML中定义了kotlin code中没有的onClick
,这是我在我的XMLandroid:onClick=“eating”
中定义的,您的答案非常清楚。(1) 因此,我应该将位置R.drawable.after\u cookie
放入displayImage()
为什么?(2) 我尝试过其他方法,请参考上面的问题。每次我点击按钮,它就会崩溃。(3) 为什么我不能使用findViewById(R.id.before\u cookie)作为ImageView
?(4) 我必须在您的代码ImageView=findViewById(R.id.before\u cookie)之后添加作为ImageView
,
其他wies类型不匹配:ifred类型是View!但是ImageView!预期为
,但您已经预先指定了ImageView=,为什么有必要将指定为ImageView?以下是您问题的答案:(1):您需要为图像分配一个可绘制的资源,这就是我们将资源id传递给所述方法的原因。(2) 它崩溃是因为它试图找到名为eating的方法,而您将其命名为eatCookie。(3) 如果你愿意,你可以用它。(4) 我不完全清楚为什么会发生这种情况。它应该自动转换为指定的类型。(2)fun eating(){}和fun eatCookie(){}是两种不同的方法。它们不会在我的代码中共存,因此它们不是崩溃的原因。(3) 每次我使用findViewById(R.id.before\u cookie)作为ImageView
它都会崩溃,你知道为什么吗?(5) 在我的问题中,你能看到第二种实现这一目标的方法吗?每次我使用这组代码时,它都会崩溃。这应该行得通,因为我抄袭了《为什么每次都会崩溃》?感谢您抽出时间回答我的问题。既然你花了时间,你一定认为我的问题有价值。请你投票表决这个问题好吗?这个问题将来也会对其他人有利。
05-17 12:22:11.216 3029-3029/com.example.afterclicking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.afterclicking, PID: 3029
java.lang.IllegalStateException: Could not find method eating(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)
at android.view.View.performClick(View.java:4780)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
fun eating(view: View?) {
val afterEatingText:String="I am so full!!"
displayMessage(afterEatingText)
displayImage(R.drawable.after_cookie) //Change this
}
private fun displayMessage(message: String) {
val afterEatingText = findViewById<View>(R.id.before_eating) as TextView
afterEatingText.text = message
}
private fun displayImage(imageResource: Int) { //Change this
val afterEatingImage: ImageView = findViewById<ImageView>(R.id.before_cookie)
afterEatingImage.setImageResource(imageResource)
}