Why getDrawable() doesn't work on some Android devices?

后端 未结 3 2039
耶瑟儿~
耶瑟儿~ 2021-02-07 01:16

I am getting \"nosuchmethod error\" on some user\'s phones (eg. Motorola Razr i) but it works fine on my HTC. Below is the code.

Drawable rBlack;
rBlack = getRes         


        
3条回答
  •  遥遥无期
    2021-02-07 01:43

    I'm going to bet it is failing on getDrawable(R.drawable.rblack, getTheme()); which was added in API 21. Change that line to:

    if(android.os.Build.VERSION.SDK_INT >= 21){
        rBlack = getResources().getDrawable(R.drawable.rblack, getTheme());
    } else {
        rBlack = getResources().getDrawable(R.drawable.rblack);
    }
    

提交回复
热议问题