2014-10-01 11 views
10

Sto cercando di impostare la visibilità per un pulsante come segue setVisibility():Non in grado di impostare dinamicamente il parametro

public Bundle setActivityState(Bundle bundle){ 
    startBtn = (Button) findViewById(R.id.startSensorsBtn); 

    startBtn.setVisibility(
      getVisibilityState(bundle, PersistanceConstants.START_BTN_STATE) 
    );   

    return bundle; 
} 

public int getVisibilityState(Bundle bundle, String keyName){ 
    if (bundle.getInt(keyName) == View.VISIBLE){ 
     return View.VISIBLE; 
    } else if (bundle.getInt(keyName) == View.INVISIBLE){ 
     return View.INVISIBLE; 
    } else if (bundle.getInt(keyName) == View.GONE){ 
     return View.GONE; 
    } 

    return 0; 
} 

Ma sto ottenendo l'errore:

Must be one of: View.VISIBLE, View.INVISIBLE, View.GONE less... (Ctrl+F1) 
Reports two types of problems: 
- Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something. 
- Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL. 

durante la chiamata

getVisibilityState(bundle, PersistanceConstants.START_BTN_STATE) 

Non so come aggirare questo. Comprendo che si aspetta un dato insieme di valori, ma tutto quello che so è di passare un int ad esso. Cosa si può fare qui?

+0

inserisci il tuo log degli errori .. – Sats

+0

getVisibilityState raggiungere la riga 'return 0;'? per favore, pubblica il tuo log degli errori. – wendigo

+0

Mi spiace di non averlo menzionato. Sto ottenendo questo durante la compilazione in studio Android. – jsbisht

risposta

18

Quando si sa cosa si sta facendo, è possibile sopprimere questa ispezione Studio Android a livello locale con

//noinspection ResourceType 

Per esempio,

//noinspection ResourceType 
startBtn.setVisibility(bundle.getInt(PersistanceConstants.START_BTN_STATE)); 
+1

Wow. Ha funzionato. Grazie mille laalto. – jsbisht

17

Un po 'in ritardo alla festa, ma un'altra soluzione se si stanno usando molto nel tuo codice e hai un metodo che restituisce int è quello di definire la tua annotazione di visibilità, quindi qualcosa del genere:

public class MyStuff { 

    @IntDef({View.VISIBLE, View.INVISIBLE, View.GONE}) 
    @Retention(RetentionPolicy.SOURCE) 
    public @interface Visibility { 
    } 

    public @Visibility int getVisibility() { 
     return View.GONE; 
    } 
} 

Se si esegue questa operazione, AS non si lamenterà più perché si sta restituendo un valore int corretto.

Problemi correlati