我有一个微调器,我试图在方法中获取当前选择的微调器值,该方法将在异步任务中创建一个 json 帖子,但我的 spinner.getSelectedItem() 返回 null,我不知道为什么。
我的适配器类
public class SpinnerArrayAdapter extends ArrayAdapter<Station> { private final Context context; private final List<Station> objects;
public SpinnerArrayAdapter(Context context, int resource,
List<Station> objects) {
super(context, resource, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.station_spinner_row, parent, false);
holder.v1 = (TextView) row.findViewById(R.id.spinnerTextStation);
holder.v2 = (TextView) row.findViewById(R.id.spinnerTextLine);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Station station = objects.get(position);
if (station != null) {
holder.v1.setText(station.getStation());
holder.v2.setText(station.getLine());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.station_spinner_row, parent, false);
holder.v1 = (TextView) row.findViewById(R.id.spinnerTextStation);
holder.v2 = (TextView) row.findViewById(R.id.spinnerTextLine);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Station station = objects.get(position);
if (station != null) {
holder.v1.setText(station.getStation());
holder.v2.setText(station.getLine());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
public static class ViewHolder {
TextView v1; // view1
TextView v2; // view2
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Station getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
}
这是片段中的函数,它将获取微调器选择的值但返回 null。我得到一个空指针异常,我不明白为什么。
private void sendNewWarning(View view) {
Warning war = new Warning();
Spinner stationSpinner = (Spinner) view
.findViewById(R.id.stationSpinner);
Spinner typeSpinner = (Spinner) view.findViewById(R.id.typeSpinner);
EditText descText = (EditText) view.findViewById(R.id.EditTextDesc);
war.setTown("St"); // HARDCODED!
Station stat = new Station();
Log.d("Project V", "getSelectedItemPosition: " + stationSpinner.getSelectedItemPosition());
Log.d("Project V", stationSpinner.getItemAtPosition(1).getClass().toString());
// PROBLEM HERE!
stat = (Station) stationSpinner.getItemAtPosition(stationSpinner.getSelectedItemPosition());
war.setStation(stat.getStation());
Log.d("Project V", "war.getStation(): " + war.getStation());
war.setType(typeSpinner.getSelectedItem().toString());
war.setDesc(descText.getText().toString());
String input = gson.toJson(war).toString();
new PostWarningTask(getActivity()).execute(input);
}