Вопрос

In my app I'm trying to set a Bitmap Image to FrameLayout, where the image came from SQLite database. I'm decoding that image converting it into Drawable and then setting it as background to FrameLayout but it gives me Error like java.lang.NoSuchMethodError: android.widget.FrameLayout.setBackground()

    ByteArrayInputStream imageStream2= new ByteArrayInputStream(cardbackground);
Bitmap Imagebackground = BitmapFactory.decodeStream(imageStream2);
Drawable imagebakground=new BitmapDrawable(getResources(),Imagebackground);
framelayout.setBackground(imagebakground);

I'm using,,

    android:minSdkVersion="14"
    android:targetSdkVersion="19"
Это было полезно?

Решение

setBackground() method was added into API level 16. use setBackgroundDrawable() instead....

    Drawable imagebakground = new BitmapDrawable(getResources(),Imagebackground);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        framelayout.setBackground(imagebakground);
    } else {
        frameLayout.setBackgroundDrawable(imagebakground);
    }

Другие советы

setBackground() is available from API Level 16, so having minSdkVersion set to 14 does not help. The device where you are testing it, has to be at least that api level

Set Bitmap as BackgroundDrawable to your Layout instead of setBackground()

 if (Build.VERSION.SDK_INT >= 16)
  your_layout.setBackground(...);
 else
  your_layout.setBackgroundDrawable(...);

It's becoz setBackground() is available from API Level 16

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top