Reputation: 3352
I have the following in my Fragment, and I am trying to create a spinner that displays numbers 1-5 as selection options:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootview = inflater.inflate(R.layout.fragment_create, container, false);
mAddImageButton = (Button) rootview.findViewById(R.id.add_image_button);
mSelectNumberofPollAnswers = (Spinner) rootview.findViewById(R.id.number_of_answers_spinner);
// Inflate the layout for this fragment
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
R.array.number_of_poll_answers, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
mSelectNumberofPollAnswers.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_create, container, false);
}
Strings.xml:
<string-array name="number_of_poll_answers">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
XML:
<Spinner
android:id="@+id/number_of_answers_spinner"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".3"/>
Upvotes: 1
Views: 62
Reputation: 1261
You must return rootView like this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootview = inflater.inflate(R.layout.fragment_create, container, false);
mAddImageButton = (Button) rootview.findViewById(R.id.add_image_button);
mSelectNumberofPollAnswers = (Spinner) rootview.findViewById(R.id.number_of_answers_spinner);
// Inflate the layout for this fragment
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
R.array.number_of_poll_answers, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
mSelectNumberofPollAnswers.setAdapter(adapter);
return rootview;
}
Upvotes: 1
Reputation: 258
try :
return rootview;
Instead of
return inflater.inflate(R.layout.fragment_create, container, false);
Upvotes: 1
Reputation: 39191
In your onCreateView()
method, you're returning a new, uninitialized View
in the return
statement. Instead, you want to return the View
that you inflated and initialized before that. That is, change the return
statement to:
return rootview;
Upvotes: 3