猿问

关于listviwe出错的问题?

package listview.Package;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
//import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListviewActivity extends Activity {
	private ListView list;
   // private ArrayAdapter<String> arrayadapter;
    private SimpleAdapter simpleAdapter;
    //String[]data={"慕课网1","慕课网2","慕课网3"};
    private List<Map<String, Object>> datalist;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviwe);
        list=(ListView) findViewById(R.id.listView1);
        //arrayadapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
        list.setAdapter(simpleAdapter);
        simpleAdapter=new SimpleAdapter(this,getdata(),R.layout.itme, new String[]{"img","text"},
        		new int[]{R.id.imageView1,R.id.textView1});
      
    }
    private List<Map<String, Object>> getdata(){
    	for(int i=0;i<10;i++){
    		Map<String, Object> map =new HashMap<String, Object>();
    		map.put("img", R.drawable.ic_launcher);
    		map.put("text", "慕课网"+i);
    		datalist.add(map);}
   return datalist;
    }
}

运行到模拟器,程序立马就停,logcat打印说是空指针异常,请问哪里错了,怎样改正。。!?

qq_我很丑绝缘体_0
浏览 1584回答 4
4回答

小寳_昭乂阳

先new adapter 再setAdapter() 应该就可以了

t80000

datalist没有初始化先初始化simpleAdapter,然后list.setAdapter

一步一个脚印_梦想

一楼    正解 666

qq_倚天_0

datalist没有初始化.List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
随时随地看视频慕课网APP

相关分类

Android
我要回答