更改 alpha 属性时引发错误

发布于 2024-12-20 18:00:42 字数 267 浏览 3 评论 0原文

alpha 属性设置为视图时收到此错误

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

我在代码中将

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

//with float value also doesn't works

可能是什么问题?我仅在运行时没有任何编译错误

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赢得她心 2024-12-27 18:00:42

正如文档所述,setAlpha 需要 API 11。您可能将 minSDK 设置为低于 11 的值,并在低于 11 的设备上安装/运行它。这意味着它没有此方法,因此 Java 无法找到它。

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.

不气馁 2024-12-27 18:00:42

我使用代码来设置图像本身的Alpha,而不是视图。这可以从 API 级别 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;
    }
}

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;
    }
}
江城子 2024-12-27 18:00:42

setAlpha() 此方法在 API 级别 16 中已弃用。

使用setImageAlpha(int)代替

您的 android:targetSdkVersion 可能高于 15,因此您可以在使用 setAlpha 时进行验证或者

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);
}

Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 的值为 15

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文