Java Button.setBackground(Drawable background) 抛出 NoSuchMethodError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18559248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:02:44  来源:igfitidea点击:

Button.setBackground(Drawable background) throws NoSuchMethodError

javaandroidexceptionbutton

提问by Eslam Yousef Mohammed

I'm implementing a simple method to add a Buttonto a LinearLayoutprogramatically.

我正在实现一种简单的方法来ButtonLinearLayout编程方式将 a 添加到 a 。

When I invoke the setBackground(Drawable background) method, the following Erroris thrown:

当我调用 setBackground(Drawable background) 方法时,Error抛出以下内容:

java.lang.NoSuchMethodError: android.widget.Button.setBackground

java.lang.NoSuchMethodError: android.widget.Button.setBackground

My addNewButton method:

我的 addNewButton 方法:

private void addNewButton(Integer id, String name) {

        Button b = new Button(this);
        b.setId(id);
        b.setText(name);
        b.setTextColor(color.white);
        b.setBackground(this.getResources().getDrawable(R.drawable.orange_dot));
            //llPageIndicator is the Linear Layout.
        llPageIndicator.addView(b);
}

采纳答案by Mena

You might be testing on an API below level 16 (Jelly Bean).

您可能正在测试低于 16 级的 API ( Jelly Bean)。

The setBackgroundmethod is only available from that API level onwards.

的setBackground方法只能从起该API级别。

I would try with setBackgroundDrawable(deprecated) or setBackgroundResourceif that's the case.

如果是这种情况,我会尝试使用setBackgroundDrawable(已弃用)或setBackgroundResource

For instance:

例如:

Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
Button one = new Button(this);
// mediocre
one.setBackgroundDrawable(d);
Button two = new Button(this);
// better
two.setBackgroundResource(R.drawable.ic_launcher);

回答by Nerd Dragon

You can't use setBackground(). This method may be not available in your Android level.

你不能使用setBackground(). 此方法可能不适用于您的 Android 级别。

回答by Andras Balázs Lajtha

To create a homogeneous background for a View, you can create a drawable resource of type shape, and use that with the setBackgroundResource.

要为视图创建同质背景,您可以创建一个 shape 类型的可绘制资源,并将其与 setBackgroundResource 一起使用。

red_background.xml

red_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#FF0000"/>    
</shape>

Activity:

活动:

Button b = (Button)findViewById(R.id.myButton);
b.setBackgroundResource(R.drawable.red_background);

But this will look pretty bad, flat and out of place. If you want a colored button that looks like a button, than you can either design it yourself (rounded corners, stroke, gradient fill...) or a fast and dirty solution is to add a PorterDuff filter to the button's background:

但这看起来很糟糕,平坦且不合适。如果你想要一个看起来像按钮的彩色按钮,那么你可以自己设计它(圆角、描边、渐变填充......),或者快速而肮脏的解决方案是在按钮的背景中添加一个 PorterDuff 过滤器:

Button b = (Button)findViewById(R.id.myButton);
PorterDuffColorFilter redFilter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
b.getBackground().setColorFilter(redFilter);

回答by Sruit A.Suk

Since after Android 16 , the setBackgroundDrawable is deprecated, I suggested to checked before set code

由于在 Android 16 之后,不推荐使用 setBackgroundDrawable,我建议在设置代码之前检查

you need to check current version of Android also

您还需要检查当前的 Android 版本

Button bProfile; // your Button
Bitmap bitmap; // your bitmap

if(android.os.Build.VERSION.SDK_INT < 16) {
    bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
else {
    bProfile.setBackground(new BitmapDrawable(getResources(),bitmap));
}

回答by Ankita Bhatt

            <Button
                android:id="@+id/btnregister"
                android:layout_width="150dp"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:layout_marginHorizontal="10dp"
                android:layout_marginVertical="20dp"
                android:paddingVertical="5dp"
                style="@style/btn_register"
                android:text="Register"
                android:textColor="#FFFFFF" />

apply below code in Styles.xml file :

在 Styles.xml 文件中应用以下代码:

 <style name="btn_register">
        <item name="android:layout_marginTop">15dp</item>
        <item name="android:backgroundTint">#009688</item>
        <item name="cornerRadius">20dp</item>
    </style>