我正在尝试使用适配器进行自定义视图。但是,当我编译和调试代码时,它总是给出空指针异常。
Java代码:
ListView mListView= (ListView) findViewById(R.id.listView);
//create person objects
Users John = new Users("John","12-1-13","Male");
Users Steve = new Users("Steve","12-1-13","Male");
Users Bill = new Users("Bill","12-1-13","Male");
Users Ashley = new Users("Ashley","12-1-13","Male");
Users Matt = new Users("mATT","12-1-13","Male");
Users Zack = new Users("Zack","12-1-13","Male");
Users Perna = new Users("Perna","12-1-13","Male");
Users Simeon = new Users("Simeon","12-1-13","Male");
Users Panda = new Users("Panda","12-1-13","Male");
Users Marshal = new Users("Marshal","12-1-13","Male");
Users Mathers = new Users("Mathers","12-1-13","Male");
Users Adele = new Users("Adele","12-1-13","Male");
Users Minaj = new Users("Minaj","12-1-13","Male");
Users Oliver = new Users("Oliver","12-1-13","Male");
Users Jack = new Users("Jack","12-1-13","Male");
Users Harry = new Users("Harry","12-1-13","Male");
Users Kyle = new Users("Kyle","12-1-13","Male");
Users Micheal = new Users("mICHEAL","12-1-13","Male");
ArrayList<Users> userslist = new ArrayList<>();
userslist.add(John);
userslist.add(Jack);
userslist.add(Zack);
userslist.add(Perna);
userslist.add(Simeon);
userslist.add(Panda);
userslist.add(Marshal);
userslist.add(Adele);
userslist.add(Mathers);
userslist.add(Minaj);
userslist.add(Oliver);
userslist.add(Jack);
userslist.add(Harry);
userslist.add(Kyle);
userslist.add(Micheal);
userslist.add(Bill);
userslist.add(Steve);
userslist.add(Ashley);
userslist.add(Matt);
UserListAdapter adapter = new UserListAdapter(this,R.layout.adapter_view_layout, userslist);
mListView.setAdapter(adapter);
XML布局
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
/>