问题
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