最后的这个强制类型转换怎么回事
最后这个强制类型转换,int类型转换String类型,怎么做都报错,求解
最后这个强制类型转换,int类型转换String类型,怎么做都报错,求解
2015-12-10
public class ArrActivity extends Activity implements OnItemClickListener,OnScrollListener{
private ListView listView;
private SimpleAdapter simpleAdapter;
private ArrayAdapter<String> arr_adapter;
List<Map<String,Object>> datalist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arr);
/**ArrayList*/
// ListView lv = (ListView)findViewById(R.id.listView);
// String [] a = {"1","2","3","4"};
// arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a);
// lv.setAdapter(arr_adapter);
/**SimpleAdapter*/
ListView listv = (ListView) findViewById(R.id.listView);
datalist = new ArrayList<Map<String,Object>>();
String []from = {"icon","title","content"};
int [] to = {R.id.image,R.id.title,R.id.content};
simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, from, to);
listv.setAdapter(simpleAdapter);
listv.setOnItemClickListener(this);
listv.setOnScrollListener(this);
}
public List<Map<String,Object>> getData(){
for (int i = 0; i < 20; i++) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("icon", R.drawable.ic_launcher);
map.put("title", "标题"+ (i+1));
map.put("content", "内容" + (i+1));
datalist.add(map);
}
return datalist;
}
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
}
public void onScrollStateChanged(AbsListView arg0, int arg1) {
}
/**此处出错,Logcat报错,
* java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object
* android.widget.ListView.getItemAtPosition(int)' on a null object reference
*/
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = listView.getItemAtPosition(position).toString(); //position对应的选项
Toast.makeText(ArrActivity.this, text, 3000).show();
}
原因可能是getItemAtPosition那里出了问题,帮忙看一下,谢谢
举报