I use the SetBackground method when I use the Android version 4.0.4, the program crashes.
Tip:caused By:java.lang.NoSuchMethodError:android.widget.ImageView.setBackground
All information is related to the Android version of the system.
By looking for a source greater than or equal to API16
First Look at setbackgrounddrawable
Tip This method is obsolete and has been replaced by the SetBackground method
Let's take a look at the SetBackground method
In the source code less than API16, only found the Setbackgrounddrawable method, did not find SetBackground method
thereby obtaining
(1) After API16, the Android system is replaced by the SetBackground method Setbackgrounddrawable method, but after viewing the source code, The Setbackgrounddrawable method is called directly in the SetBackground method, so setbackground can be used after API16, or setbackgrounddrawable can be used, However, using the setbackgrounddrawable hint is obsolete.
(2) Before the API, the Android value provides the Setbackgrounddrawable method.
So we can make judgments in the program.
/** * Use setbackgrounddrawable before API16, use SetBackground after API16 api16< ---->4.1 * @param view * @param drawable */ private void setbackgroundofversion (view view, drawable drawable) { if ( Build.VERSION.SDK_INT >= build.version_codes. Jelly_bean) { //android system is greater than or equal to API16, using SetBackground view.setbackground (drawable); } else { // Android system is less than API16, use SetBackground view.setbackgrounddrawable (drawable); } }
The difference between SetBackground and setbackgrounddrawable