The difference between SetBackground and setbackgrounddrawable

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.