Button.setBackground(Drawable background) throws NoSuchMethodError

前端 未结 5 1723
故里飘歌
故里飘歌 2021-02-04 08:57

I\'m implementing a simple method to add a Button to a LinearLayout programatically.

When I invoke the setBackground(Drawable background) metho

5条回答
  •  终归单人心
    2021-02-04 09:37

    Since after Android 16 , the setBackgroundDrawable is deprecated, I suggested to checked before set code

    you need to check current version of Android also

    Button bProfile; // your Button
    Bitmap bitmap; // your bitmap
    
    if(android.os.Build.VERSION.SDK_INT < 16) {
        bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
    }
    else {
        bProfile.setBackground(new BitmapDrawable(getResources(),bitmap));
    }
    

提交回复
热议问题