package com.example.joeys.myapplication;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private SimpleAdapter simp_adapter;
private List<Map<String, Object>> list;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simp_adapter = new SimpleAdapter(this, getData(), R.layout.test, new String[]{"iv", "text"}, new int[]{R.id.iv, R.id.text});//这行也报错
list = new ArrayList<Map<String, Object>>();
listview.setAdapter(simp_adapter);
}
private List<Map<String, Object>> getData() {
for (int i = 0; i < 10; i++) {
Map<String,Object>map=new HashMap<String, Object>();
map.put("iv",R.drawable.cons);
map.put("text","ceshi"+i);
list.add(map);//这行报错
}
return list;
}
}
错误信息如下: 跟着视频写一个简单的simpleAdapter但是就一直出错怎么办啊
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.joeys.myapplication.MainActivity.getData(MainActivity.java:36)
at com.example.joeys.myapplication.MainActivity.onCreate(MainActivity.java:25)
------解决思路----------------------
你在getData的时候,list = new ArrayList<Map<String, Object>>();这句话还没有运行。list为空。所以把这句话放到getData上面就可以了。