Spinner在Fragment中不起作用

时间:2017-02-02 05:33:10

标签: android spinner

当我在Fragment中使用Spinner时,我的应用程序崩溃了。

public class TestRideFragment extends Fragment implements AdapterView.OnItemSelectedListener {

    String[] country = { "India", "USA", "China", "Japan", "Other",  };

    public TestRideFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        //return inflater.inflate(R.layout.fragment_test_ride, container, false);
        View v = inflater.inflate(R.layout.fragment_test_ride, container, false);

        Spinner spin = (Spinner) getView().findViewById(R.id.spinner1);
        spin.setOnItemSelectedListener(this);

        //Creating the ArrayAdapter instance having the country list
        ArrayAdapter aa = new ArrayAdapter(getContext(),android.R.layout.simple_spinner_item,country);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spin.setAdapter(aa);

        return v;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) { }
 }

有人可以帮我发现我的代码问题吗?

4 个答案:

答案 0 :(得分:1)

您正在尝试在创建之前获取视图,这就是它返回null的原因。

所以,你可以这样做,

Spinner spin = (Spinner) v.findViewById(R.id.spinner1);

或者,您可以将代码移动到onViewCreated,其中将创建视图,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

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



}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Spinner spin = (Spinner) view.findViewById(R.id.spinner1);
    spin.setOnItemSelectedListener(this);

    //Creating the ArrayAdapter instance having the country list
    ArrayAdapter aa = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, country);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //Setting the ArrayAdapter data on the Spinner
    spin.setAdapter(aa);
}

答案 1 :(得分:0)

尝试使用v.findViewById(R.id.spinner1)同时确保在xml布局中声明了spinner1 fragment_test_ride

尝试在strings.xml中添加数组

<array name="countries">
    <item>India</item>
    <item>USA</item>
    <item>China</item>
    <item>Japan</item>
    <item>Other</item>
</array>

和你的xml

<android.support.v7.widget.AppCompatSpinner 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"
   android:entries="@array/countries" 
   android:id="@+id/spinner1" />

删除ArrayAdapter但不要这样做会根据字符串xml

中声明的数组中的whats使您的选项保持静态

答案 2 :(得分:0)

更改您的代码:

Spinner spin = (Spinner) getView().findViewById(R.id.spinner1);

进入这个:

Spinner spin = (Spinner) v.findViewById(R.id.spinner1);

答案 3 :(得分:0)

也许问题在你的适配器中应该是;

   ArrayAdapter<CharSequence> aa= ArrayAdapter.createFromResource(this,
    country, android.R.layout.simple_spinner_item);

你有;

   ArrayAdapter<CharSequence> aa= ArrayAdapter.createFromResource(this,
     android.R.layout.simple_spinner_item, country);