Error thrown on changing the alpha property

℡╲_俬逩灬. 提交于 2019-12-12 20:09:53

问题


I get this error for setting the alpha property to a view

java.lang.NoSuchMethodError: android.view.View.setAlpha

in my code

((View) findViewById(R.id.view)).setAlpha(100);

//with float value also doesn't works

What might be the problem ? I do not have any compile errors just on runtime


回答1:


As the documentation states, setAlpha requires API 11. You probably have your minSDK set to something below 11 and are installing/running this on a device which is below 11. That means it doesn't have this method, so Java can't find it.




回答2:


I used code to setAlpha of the image itself, not the view. This is available from API level 1..

public void toggleButton(int i) {
    if (indImageBtnEnabled[i]) {

        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(25);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = false;
    } else {
        // findViewById(myImagebtns[i]).setAlpha(1f) << NEEDS API11;
        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(255);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = true;
    }
}



回答3:


setAlpha() This method was deprecated in API level 16.

use setImageAlpha(int) instead

probably you have an android:targetSdkVersion higher than 15, so you can validate when use setAlpha or

if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
    ((View) findViewById(R.id.view)).setImageAlpha(100);
    } else {
    ((View) findViewById(R.id.view)).setAlpha(100);
}

the value of Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 is 15



来源:https://stackoverflow.com/questions/8478924/error-thrown-on-changing-the-alpha-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!