java.lang.NoSuchMethodError:setBackground()

时间:2014-03-12 12:00:53

标签: android android-drawable android-framelayout

在我的应用中,我正在尝试将位图图像设置为FrameLayout,其中图像来自SQLite数据库。我正在解码该图像,将其转换为Drawable,然后将其设置为FrameLayout的背景,但它给出了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);

我正在使用,,

    android:minSdkVersion="14"
    android:targetSdkVersion="19"

3 个答案:

答案 0 :(得分:4)

setBackground()方法已添加到API级别16.使用setBackgroundDrawable()代替......

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

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

答案 1 :(得分:2)

API等级16提供了

setBackground(),因此将minSdkVersion设置为14无效。您正在测试它的设备必须至少达到api级别

答案 2 :(得分:2)

Bitmap设为BackgroundDrawableLayout而不是setBackground()

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

它的becoz setBackground()可从API Level 16获得