2015-08-21 20 views
6

Sto salvando e ripristinando la visibilità di una vista in una delle mie attività. Faccio questo chiamando mButton.getVisibility() e salvando questo in un Bundle. In onRestore, dove ottengo il valore int, mostra un errore.Deve essere uno di: View.VISIBLE, View.INVISIBLE, View.GONE

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. 

Il codice compilato ed eseguito senza errori

codice

@Override 
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { 
    savedInstanceState.putInt("BUTTON_VISIBILITY", mButton.getVisibility()); 

    super.onSaveInstanceState(savedInstanceState); 
} 

public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 

    mButton.setVisibility(savedInstanceState.getInt("BUTTON_VISIBILITY")); 
    // savedInstanceState.getInt("BUTTON_VISIBILITY") is underlined red 
} 
+2

È possibile aggiungere '@SuppressWarnings ("ResourceType")' – BNK

+0

Normalmente un avviso viene sottolineata giallo, questo è sottolineato rosso mi ha causato qualche preoccupazione – Eoin

+1

@SuppressWarnings ("ResourceType") ha funzionato! grazie – Eoin

risposta

13

Come ho appena commentato, è possibile aggiungere @SuppressWarnings("ResourceType"). Spero che questo ti aiuti!

Alt-Enter enter image description here

+1

Sì, ha fatto il lavoro, grazie – Eoin

+0

La mia esperienza è che: quando AS avvisa, premi Alt-Invio per trovare una soluzione :) – BNK

+0

Alt-Invio non ha avuto nulla per questo:/ – Eoin

Problemi correlati