2016-07-04 102 views
1

我在片段下面,我試圖創建一個顯示數字1-5作爲選擇選項的微調:微調顯示在XML,但不是在加載應用程序

@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"/> 
+0

您需要返回'onCreateView()'的'rootview'。就像你現在所做的那樣,你正在返回一個不同的,新增的,未初始化的View。 –

+1

謝謝!想要提交作爲答案,我可以將其標記爲已接受? – tccpg288

回答

3

在你onCreateView()方法,你就返回一個新的,未初始化Viewreturn聲明中。相反,您希望返回在此之前膨脹並初始化的View。也就是說,return聲明更改爲:

return rootview; 
1

嘗試:

return rootview; 

而不是

return inflater.inflate(R.layout.fragment_create, container, false); 
1

您必須返回rootView這樣

@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; 
} 
相關問題