Button.setBackground(Drawable background) throws NoSuchMethodError

前端 未结 5 1724
故里飘歌
故里飘歌 2021-02-04 08:57

I\'m implementing a simple method to add a Button to a LinearLayout programatically.

When I invoke the setBackground(Drawable background) metho

5条回答
  •  终归单人心
    2021-02-04 09:34

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

    The setBackground method is only available from that API level onwards.

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

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

提交回复
热议问题