0

I've been trying to solve a problem which i been having for past week , i am trying to a access spinner inside different xml layout, all i want is to access the spinner and add array to the spinner.

This is the code i am using and it doesn't work

setContentView(R.layout.mainreg);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,false);

Spinner spin = (Spinner) view123.findViewById(R.id.spinnerproblem);
ArrayAdapter<String> adapter = 
    new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,items);    

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
mainLayout.addView(view123);
j0k
  • 22,600
  • 28
  • 79
  • 90
ben
  • 29
  • 1
  • 7
  • the problem is i cant access the spinner so i can add "item" array also i can add setOnItemSelectedListener to the spinner . the only problem i could think of is just i cant access the spinner . – ben Aug 16 '12 at 13:14
  • What kind of exception do you see in Logcat? Also it's not clear what's the point of adding a spinner the way you do, why not declare it in your mainreg xml file as child of main_layout_id? – Anton Aug 16 '12 at 13:19
  • i want to do it this way is because i got fragment layout which is linked to three different xml layouts. so in my main class i make the fragments so i can slide throw the pages . now i am facing this problem because the spinner is in different xml layout. many thanks Anton – ben Aug 16 '12 at 13:25
  • you should probably utilize the layout trick instead of messing with controls management in code. See [here](http://developer.android.com/training/improving-layouts/reusing-layouts.html) for details – Anton Aug 16 '12 at 14:01
  • I tried that but it didn't work – ben Aug 16 '12 at 14:50

1 Answers1

0

pass true for attach to root while inflating layout, like this.

View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,true);
Chitranshu Asthana
  • 1,089
  • 8
  • 19
  • 08-16 13:54:35.535: E/AndroidRuntime(9264): java.lang.RuntimeException: Unable to start activity ComponentInfo{police.app/police.app.MainReg}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 08-16 13:54:35.535: E/AndroidRuntime(9264): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 08-16 13:54:35.535: E/AndroidRuntime(9264): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) .......... – ben Aug 16 '12 at 13:00
  • Sorry.. i think you should remove the following line. mainLayout.addView(view123); – Chitranshu Asthana Aug 16 '12 at 14:55