Home > Software engineering >  Why i cant find by id xml image
Why i cant find by id xml image

Time:05-26

public class MainActivity extends AppCompatActivity {

ListView listView;
Button buttonForCP;
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonForCP =(Button) findViewById(R.id.buttonForCP);
    backButtonZer = findViewById(R.drawable.ic_img);//this problem 
                                // ||||||||||||||||||||||||||
    buttonForCP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    buttonForCP.setBackground(img);
        }
    });
}

for some reason can not read xml image the compiler writes "Expected resource of type id" I googled nothing found help please

CodePudding user response:

I assume backButtonZer is a Button, you need to replace R.drawable.ic_img with R.id.backButtonZer

backButtonZer = findViewById(R.id.backButtonZer);

CodePudding user response:

As the method name findViewById() says, you can only find views by id, not a drawable or something else. backButtonZer - is it a button? what do you want to do with a drawable?

to change the background of the button buttonForCP to R.drawable.ic_img you can use buttonForCP.setBackground(R.drawable.ic_img)

  • Related