对照着视频自己写了代码,但一运行就闪退了,检查了好久都没找到问题,代码感觉差不多啊,麻烦老师帮我看下好么
package com.test.spinner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.test.spinner.R.id;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{
private Spinner spinner;
private SimpleAdapter adapter;
private List<Map<String,Object>>datalist;
private TextView text;
private String cityName[]={"北京","上海","广州","深圳","厦门"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner=(Spinner) findViewById(R.id.spinner);
text=(TextView) findViewById(R.id.text);
text.setText("您的选择城市是北京");
datalist=new ArrayList<Map<String,Object>>();
adapter=new SimpleAdapter(this, getdata(), R.layout.item, new String[]{"img", "city"},new int[]{R.id.img,R.id.city});
adapter.setDropDownViewResource(R.layout.item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
private List<Map<String,Object>> getdata() {
for(int i=0;i<cityName.length;i++){
Map<String,Object>map=new HashMap<String,Object>();
map.put("img", R.drawable.ic_launcher);
map.put("city",cityName[i]);
datalist.add(map);
}
return datalist;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String city1=(String) adapter.getItem(position);
text.setText("您选择的城市是"+city1);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}