android开发SimpleAdapter的代码问题
publicclassMainActivity2extendsActionBarActivity{privateListViewlistView=null;private...
public class MainActivity2 extends ActionBarActivity {
private ListView listView = null;
private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
private SimpleAdapter simpleAdapter = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
initView();
}
private void initView(){
listView = (ListView) super.findViewById(R.id.listView);
settingAdapter();
listView.setAdapter(simpleAdapter);
}
private void settingAdapter(){
initList();
//key in map
String from[] = new String[] {"image", "time", "description"};
//component id in xml
int to[] = new int[] {R.id.image, R.id.time, R.id.description};
simpleAdapter = new SimpleAdapter(this, list, R.layout.activity_main_activity2, from, to);
}
private void initList(){
int[] res = new int[] {R.drawable.koala, R.drawable.penguins, R.drawable.tulips};
String string[][] = new String[][]{{"ti", "1-des-Koala"},{"ti","2-des-Penguins"},{"ti","3-des-Tulips"},};
for(int i = 0; i<3; i++){
HashMap<String, Object> map = new HashMap<String,Object>();
map.put("image", res[i]);
map.put("time", string[i][0]);
map.put("description", string[i][1]);
list.add(map);
}
}
}
报错的位置是在initView和onCreate里
错误内容是Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
求高手指点! 展开
private ListView listView = null;
private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
private SimpleAdapter simpleAdapter = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
initView();
}
private void initView(){
listView = (ListView) super.findViewById(R.id.listView);
settingAdapter();
listView.setAdapter(simpleAdapter);
}
private void settingAdapter(){
initList();
//key in map
String from[] = new String[] {"image", "time", "description"};
//component id in xml
int to[] = new int[] {R.id.image, R.id.time, R.id.description};
simpleAdapter = new SimpleAdapter(this, list, R.layout.activity_main_activity2, from, to);
}
private void initList(){
int[] res = new int[] {R.drawable.koala, R.drawable.penguins, R.drawable.tulips};
String string[][] = new String[][]{{"ti", "1-des-Koala"},{"ti","2-des-Penguins"},{"ti","3-des-Tulips"},};
for(int i = 0; i<3; i++){
HashMap<String, Object> map = new HashMap<String,Object>();
map.put("image", res[i]);
map.put("time", string[i][0]);
map.put("description", string[i][1]);
list.add(map);
}
}
}
报错的位置是在initView和onCreate里
错误内容是Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
求高手指点! 展开
1个回答
展开全部
你的simpleAdapter = new SimpleAdapter(this, list, R.layout.activity_main_activity2, from, to);这个并不是static的,所以在listView.setAdapter(simpleAdapter);的时候会找不到所谓的sinpleAdapter
更多追问追答
追问
那应该怎样修改呢?我是个新手。。。
追答
。。写到外面去啊,先申明这个simpleAdapter,写到Activity里面
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |