2013-09-01 15 views
11

Sto implementando un metodo semplice per aggiungere uno a LinearLayout in modo programmatico.Button.setBackground (Sfondo disegnabile) genera NoSuchMethodError

Quando invoco il metodo setBackground (sfondo Drawable), il seguente Error è gettato:

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

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

risposta

29

Si potrebbe essere testando su un API sotto il livello 16 (Jelly Bean).

Il metodo setBackground è disponibile solo da quel livello API in poi.

Vorrei provare con setBackgroundDrawable (obsoleto) o setBackgroundResource se questo è il caso.

Per esempio:

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

Quindi, qual è la soluzione per aggiungere la sfondo del pulsante programmaticamente per abbassarsi rispetto a Api 16..withou t setBackGroundDrawable..which è deprecato ...? –

+1

@EslamYousefMohammed ha modificato la mia risposta: prova 'setBackgroundResource'. – Mena

+0

ho impostato android: minSdkVersion = "9" nel mio manifest, perché eclipse non mi avvisa di questo? – wutzebaer

-1

Non è possibile utilizzare setBackground(). Questo metodo potrebbe non essere disponibile nel tuo livello Android.

+0

FYI: Nella sua forma attuale, questo è più di un [commento] (http://stackoverflow.com/help/privileges/comment) di una risposta. (Questo è probabilmente il motivo per cui è stato rifiutato). Una "risposta" dovrebbe anche includere possibili alternative alla risoluzione del problema, [come la risposta accettata] (http://stackoverflow.com/a/18559344/104223). Vedi [Rispondi]. – Leigh

1

Per creare uno sfondo omogeneo per una vista, è possibile creare una risorsa drawable di tipo shape e utilizzarla con setBackgroundResource.

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> 

attività:

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

Ma questo cercherà piuttosto male, piatta e fuori luogo. Se vuoi un pulsante colorato che assomiglia a un pulsante, puoi progettarlo tu stesso (angoli arrotondati, tratto, riempimento sfumato ...) o una soluzione veloce e sporca è aggiungere un filtro PorterDuff allo sfondo del pulsante:

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

Dal momento che dopo Android 16, il setBackgroundDrawable è deprecato, ho suggerito di controllato prima serie di codici

è necessario controllare la versione attuale di Android anche

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