Error thrown on changing the alpha property

2019-07-19 03:56发布

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

3条回答
beautiful°
2楼-- · 2019-07-19 04:33

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楼-- · 2019-07-19 04:39

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.

查看更多
老娘就宠你
4楼-- · 2019-07-19 04:54

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

查看更多
登录 后发表回答