Pour une application Android, j'essaie d'utiliser un Spinner qui, lorsqu'une option est sélectionnée, masque/affiche les objets View pertinents. Pour mon application, ces objets sont un EditText et un libellé TextView associé pour le champ. Malheureusement, je n'arrive pas à obtenir l'EditText pour masquer/afficher, et quand j'ajoute du code pour cacher/afficher le TextView, j'obtiens une exception NullPointerException. Je suppose que puisque je dispose les objets de vue dans un RelativeLayout, en cachant l'un des objets de vue, je supprime sa relation avec d'autres objets de vue, d'où le NullPointer.Android - comment utiliser Spinner pour masquer/afficher Afficher les objets
Quelqu'un peut-il comprendre pourquoi cela pourrait se produire? Voici mon code:
public class FormFields extends Activity {
private Spinner mSpinner;
private EditText mTextField;
private TextView mLabel;
private static final int SPINNER_OPTION_FIRST = 0;
private static final int SPINNER_OPTION_SECOND = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_fields);
mTextField = (EditText) findViewById(R.id.text_field);
mLabel = (TextView) findViewById(R.id.field_label)
mSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.spinnerOptions, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter1);
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
switch(position) {
case SPINNER_OPTION_FIRST: {
mLabel.setVisibility(View.GONE);
mTextField.setVisibility(View.GONE);
}
case SPINNER_OPTION_SECOND: {
mLabel.setVisibility(View.VISIBLE);
mTextField.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// Do nothing
}
});
}
}
form_fields.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#104667">
<TextView
android:id="@+id/spinner_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:textStyle="bold"
android:text="Please select an option" />
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner_label"
android:layout_marginLeft="25dip"
android:layout_marginRight="25dip"
android:drawSelectorOnTop="true"
android:prompt="@string/spinnerPrompt" />
<TextView
android:id="@+id/field_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:textStyle="bold"
android:text="Enter text here: "
android:visibility="gone" />
<EditText
android:id="@+id/text_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:layout_marginRight="25dip"
android:layout_below="@+id/field_label"
android:visibility="gone" />
</RelativeLayout>
</ScrollView>
Merci beaucoup. Comme ce code était légèrement ajusté à partir de mon application, le point-virgule n'était pas le problème réel (sinon, mon application n'aurait pas été compilée). En fait, je n'avais pas du tout défini ma variable membre mLabel! Allez comprendre. Je suis curieux - quel est le but de la déclaration de rupture? – Keeb13r
Sans les instructions 'break;' séparant chaque instruction 'case', l'exécution passe à chaque instruction' case' successivement. Il y a des raisons pour lesquelles vous pourriez vouloir que cela se produise, mais habituellement un bloc 'switch' est utilisé pour fournir des options alternatives et chaque' case' devrait seulement exécuter le code pour cette condition particulière. Pour quelques exemples et une explication détaillée de l'instruction 'switch', voir: http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Brian