http://i.stack.imgur.com/HGsTw.jpg 这是我得到的错误,应用程序崩溃了,什么也没显示!。
PS:当我将最后 4 行代码从 OnCreate 方法移动到 OnItemSelected 方法的开头时,我没有收到任何错误,并且此布局(http://i.stack.imgur.com/1DiLI.jpg)显示但没有下拉菜单列表(当我单击背景旁边的箭头按钮时,我希望有一个颜色列表)。
这是我的代码:
public class SetLayout extends Activity implements OnItemSelectedListener {
TextView o_markertv, x_markertv, bcktv, gridtv;
String[] colors ={"Red", "Green", "Blue", "Black", "White"};
Spinner spinner1, spinner2, spinner3, spinner4;
LinearLayout bck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setlayout);
bck= (LinearLayout) findViewById(R.id.bck);
o_markertv= (TextView) findViewById(R.id.o_markertv);
x_markertv= (TextView) findViewById(R.id.x_markertv);
bcktv= (TextView) findViewById(R.id.bcktv);
gridtv= (TextView) findViewById(R.id.gridtv);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
//Button BtnSubmit = (Button) findViewById(R.id.btnSubmit);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (SetLayout.this,android.R.layout.simple_spinner_item,colors);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position= spinner1.getSelectedItemPosition();
switch(position){ //red, green blue black white
case 0:
bck.setBackgroundColor(color.red);
break;
case 1:
bck.setBackgroundColor(color.green);
break;
case 2:
bck.setBackgroundColor(color.blue);
break;
case 3:
bck.setBackgroundColor(color.black);
break;
case 4:
bck.setBackgroundColor(color.white);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}