当我尝试在imageView的背景上设置位图时出现致命异常

时间:2014-04-11 12:27:51

标签: android exception bitmap imageview android-drawable

我得到了

FATAL EXCEPTION: main
    java.lang.NoSuchMethodError: android.widget.ImageView.setBackground
    at ua.khuta.mobilereception.ProfileActivity.onCreate(ProfileActivity.java:117)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
    at android.app.ActivityThread.access$600(ActivityThread.java:122)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4340)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

在这一行

imageView.setBackground(new BitmapDrawable(getResources(),k));

所有在Android 4.4.2上工作正常,但它不适用于较低版本。我的部分内容是

  <uses-sdk android:minSdkVersion="10"
        android:targetSdkVersion="17"/>

1 个答案:

答案 0 :(得分:6)

请试试这个:

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    //if you want take image from resources - getResources().getDrawable(your_id)
    imageView.setBackgroundDrawable(new BitmapDrawable(getResources(),k)));

} else {
    imageView.setBackground(new BitmapDrawable(getResources(),k));
}