How to change a photo?

时间:2019-01-18 18:52:21

标签: java android

I am trying to change photos in android studio by clicking on my button. When I put code for changing the photo in my MainActivity.java I keep getting this type of error messages and it says : Cannot resolve symbol "image"

image.setImageResource(R.drawable.xxx);

I am watching Udemy course for android development and I have done everything same like the professor on that video.

I have tried to restart android studio. I have tried to make new project. I have tried to clear invalidate caches and restart.

public void changeImage(View view)
{
    ImageView bitcoin = findViewById(R.id.bitcoin);
    image.setImageResource(R.drawable.xxx);


}

I hope there is actual error with android studio,because code is clone of the video that I am watching.

3 个答案:

答案 0 :(得分:1)

Set Your Code Like this

ImageView image = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);

答案 1 :(得分:1)

您正在使用bitcoin变量将布局的ImageView绑定到Java文件中,并且尝试在未知变量“ image”上设置图像(可能未在类中定义)。因此,您必须进行如下设置。

ImageView bitcoin = findViewById(R.id.bitcoin); bitcoin.setImageResource(R.drawable.xxx);

答案 2 :(得分:0)

change your this line

image.setImageResource(R.drawable.xxx)

to this one:

bitcoin.setImageResource(R.drawable.xxx)