0

My application supports android version 2.3.3 (SDK 10) and above. I have a simple code:

private void setBackgroundToView(View view, Drawable drawable) {
    if (Build.VERSION.SDK_INT >= 16) {
        view.setBackground(drawable);
    } else {
        view.setBackgroundDrawable(drawable);
    }
}

I found that on GT-I9100G with android 4.0.3 which would have sdk version 15 follow log:

I/dalvikvm(13683): Could not find method android.view.View.setBackground, referenced from method %some_package%.setBackgroundToView

It means that Build.VERSION.SDK_INT has value more than 15.

  1. How can I prevent illegal calling of unsupported method in this case?

  2. Have every devices and os versions with firmwares same sdk_int for same SDK versions?

  3. Can I use Build.VERSION.RELEASE to additional check number of version?

4

4 回答 4

4

Note that the log is tagged I for information, not E for error.

The class loader loads the code and while verifying the bytecode, it finds a call to a missing method and logs it. The code itself is not executed, you would get NoSuchMethodError if it was.

Your SDK_INT value works correctly.

于 2013-09-05T11:13:59.347 回答
0

Why not just use setBackgroundDrawable always?

Also, you should debug the SDK-INT version of that device, instead of only assuming it is 15 or so.

于 2013-09-05T09:52:29.580 回答
0

As previously mentioned, i would just use setBackgroundDrawable as you dont have any advantages of setBackground, anyways.

If you still want to use it. Recheck the SDK version of your device. I dont know any device where Build.VERSION.SDK_INT isnt set properly. You could also do a try/catch and fall back to setBackgroundDrawable if setBackground raises an exception.

于 2013-09-05T10:03:02.597 回答
0

I use this code:

    if (Build.VERSION.SDK_INT<16){
        rl.setBackgroundDrawable(getResources().getDrawable(image));
    } else {
        rl.setBackground(getResources().getDrawable(image));
    }

Ok, you can always use setBackgroundDrawable as mentioned above, but if you don't, code above works perfectly.

于 2013-09-05T10:12:32.687 回答