我正在尝试显示一个Spinner
列表供用户选择,然后将选择绑定到另一个array
. 保存用户选择的值surfaceCode
以供以后使用。Spinner
数组R.array.surface_option
和要绑定的数组对齐R.array.surface_code
并保存在xml中。
这是我的代码...
spinnerSurface = (Spinner) findViewById(R.id.spinnerSurface);
ArrayAdapter<CharSequence> adapterSurface = ArrayAdapter.createFromResource(this, R.array.surface_option, android.R.layout.simple_spinner_item);
adapterSurface.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSurface.setAdapter(adapterSurface);
spinnerSurface.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
TextView tx = (TextView)v;
Log.i("\n\nid",String.valueOf(tx.getText()));
String surfaceCode = getResources().getStringArray(R.array.surface_code)[spinnerSurface.getSelectedItemPosition()];
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Log.d("code outside", surfaceCode.trim() + " is equal to SW: " + surfaceCode.trim().equals("SW"));
出现surfaceCode
错误process stopped unexpectedly
,可能是因为它返回null
。我的代码有什么问题?