Abdel Hidalgo :
I have a spinner that allows me to select a particular text. I am creating a second spinner programmatically and putting (in it) differents arrays of string elements depending on the choice made in a switch activated when selecting something from the first spinner. The second spinner is inside a LinearLayout. When I select one choice from the first spinner for the first time, everything is ok, when I select another choice everything crashes. Could you help me in detecting what makes this to happen?
final String[] as = {"AB", "AC", "AD", "AE", "AF", "AG","AH"};
final String[] bs = {"BC", "BD", "BE", "BF", "BG", "BH", "BI"};
Inside the onCreate method
//second Spinner
second_layout = findViewById(R.id.second_spinner);
spinner_2 = new Spinner(this);
The Switch
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String comb_selected = String.valueOf(spinner_1.getSelectedItem());
switch (comb_selected){
case "As"
second_layout.setVisibility(View.VISIBLE);
fillSecondSpinner(as);
break;
case "Bs":
second_layout.setVisibility(View.VISIBLE);
fillSecondSpinner(bs);
break;
default:
flag.setVisibility(View.INVISIBLE);
second_layout.setVisibility(View.INVISIBLE);
break;
The function that makes everything crash
public void fillSecondSpinner(String[] combinations){
ArrayAdapter<CharSequence> adapter_comb = new ArrayAdapter(this, android.R.layout.simple_spinner_item, combinations);
adapter_comb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_2.setAdapter(adapter_villes);
second_layout.addView(spinner_2);
}
This is the error I get:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.exercice1.spinner2, PID: 2304
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
at android.view.ViewGroup.addView(ViewGroup.java:4258)
at android.view.ViewGroup.addView(ViewGroup.java:4198)
at android.view.ViewGroup.addView(ViewGroup.java:4171)
at com.exercice1.spinner2.MainActivity.fillSecondSpinner(MainActivity.java:49)
at com.exercice1.spinner2.MainActivity.onItemSelected(MainActivity.java:68)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Application terminated.
Bruno Diego Martins :
Well, you need to remove the old view before a new one. Try to call this method before adding:
second_layout.removeAllViews()