Hello! Please help me why an error in R.drawable.lazer1?

public void setImgView(View v) { ImageView imgView = (ImageView) findViewById(id.lazer1); imgView.setImageDrawable(R.drawable.lazer1); } 

Specified in the xml file:

 ImageView android:layout_width="400dp" android:layout_height="50dp" android:id="@+id/lazer1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:src="@drawable/lazer1" 

moved to the drawable folder drawing lazer1.png

As an error, writes: setImageDravable (android.graphics.dravable.Dravable) in ImageView cannot be applied to (int)

What is the problem?

    3 answers 3

    Use better:

     imgView.setImageResource(R.drawable.lazer1); 
    • not better, and this is the method that the ImageView resource id accepts as input, and the one that the author of the question uses is the Dravable method. - Vitalii Obideiko

    The problem is:

    Firstly, the question has nothing to do with Android Studio , since this is just one of the IDE's developer tools. You will get exactly the same error when compiling even if you write your program in Notepad.

    Secondly, the setImageDrawable() method takes an object of type Drawable as an argument, but you are trying to slip a resource identifier that has an int type and is not suitable as an argument for this method.

    In order for ImageView take as an argument the resource identifier, as you want, you should use another method - setImageResource()

    Thirdly, since you remembered Android Studio , this IDE provides comprehensive information on method signatures and suggests what type of arguments one or another of them expects, just so that the programmer does not make such mistakes that happened to you. The possibilities of modern development tools can (and should) be used.

      Insert not R.drawable.lazer1, but Bitmap.convertFromResource (R.drawable.lazer1) ... i.e. insert not int, but Bitmap