错误信息:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.example.ListViewTest.MyActivity.Getdata(MyActivity.java:38)
at com.example.ListViewTest.MyActivity.onCreate(MyActivity.java:26)
package com.example.ListViewTest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.*;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
private ListView L1;
private SimpleAdapter A2;
private List<Map<String,Object>> Data_List;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item);
L1 = (ListView) findViewById(R.id.listView);
Data_List = new ArrayList<Map<String,Object>>();
A2 = new SimpleAdapter(this,Getdata(),R.layout.item,new String[] {"pic","introduction"},new int[] {R.id.imageView,R.id.textView});
L1.setAdapter(A2);
}
private List<Map<String,Object>> Getdata(){
for ( int i = 0;i < 20;i++){
Map<String,Object> map = new HashMap<String,Object>();
map.put("pic",R.drawable.ic_launcher);
map.put("introduction","陶融" + i);
Data_List.add(map);
}
return Data_List;
}
}