public class MainActivity extends AppCompatActivity { private ListView listView; private SimpleAdapter simpleAdapter; private List<Map<String,Object>> personinfo; Test test; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test = new Test(this); listView = (ListView) findViewById(R.id.list); personinfo=new ArrayList<Map<String,Object>>(); simpleAdapter = new SimpleAdapter(this, getpersoninfo(), R.layout.item, new String[]{"pic", "text","age","sex"}, new int[]{R.id.pic, R.id.word,R.id.age,R.id.sex}); listView.setAdapter(simpleAdapter); } public Person[] getperson() { Person[] persons=new Person[100]; int i=0; SQLiteDatabase db=test.getReadableDatabase(); Cursor cursor=db.rawQuery("select * from info",null); if(cursor!=null&&cursor.getCount()>0){ while (cursor.moveToNext()){ Person person=new Person(); person.setName(cursor.getString(1)); person.setSex(cursor.getString(2)); person.setAge(cursor.getString(3)); person.setImage(cursor.getString(4)); persons[i]=person; i++; } } cursor.close(); test.close(); return persons; } public List<Map<String,Object>> getpersoninfo(){ Person[] persons=getperson(); for(int i=0;i<persons.length;i++){ Map<String,Object> map=new HashMap<String, Object>(); map.put("pic",persons[i].image); map.put("text",persons[i].name); map.put("age",persons[i].age); map.put("sex",persons[i].sex); personinfo.add(map); } return personinfo; } }
报错:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fans.listview/com.example.fans.listview.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5069)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.fans.listview.MainActivity.getpersoninfo(MainActivity.java:60)
at com.example.fans.listview.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5069)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
不会编程的划水员
相关分类