我只是尝试使用复选框创建适配器到我的列表,我正在使用Realm作为我的数据库。
我找到了空对象引用。 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.View android.app.Activity.findViewById(int)'。
public class DialogActivity extends AppCompatActivity {
Realm realm;
private RealmList<DetailPerson> person;
private ListView listView;
private MenuAdapter adapter;
private String bill;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_add_menu);
bill = getIntent().getStringExtra("bill_ID");
realm = Realm.getDefaultInstance();
listView = (ListView) findViewById(R.id.listViewMenu);
person = realm.where(Bill.class).equalTo("Bill_ID", bill).findFirst().getDetailperson();
adapter = new MenuAdapter(this,R.layout.row_menu,person);
listView.setAdapter(adapter);
}
}
Bill.java
public class Bill extends RealmObject {
@PrimaryKey private String Bill_ID;
public RealmList<DetailMenu> detailmenu;
public RealmList<DetailPerson> detailperson;
private String name;
private int price;
public RealmList<DetailPerson> getDetailperson() {
return detailperson;
}
public void setDetailperson(RealmList<DetailPerson> detailperson) {
this.detailperson = detailperson;
}
public RealmList<DetailMenu> getDetailmenu() {
return detailmenu;
}
public void setDetailmenu(RealmList<DetailMenu> detailmenu) {
this.detailmenu = detailmenu;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) { this.price = price; }
public String getBill_ID() {
return Bill_ID;
}
public void setBill_ID(String bill_ID) {
Bill_ID = bill_ID;
}
}
MenuAdapter.java
public class MenuAdapter extends ArrayAdapter<DetailPerson>{
private RealmList<DetailPerson> persons;
private Context context;
Realm realm;
private LayoutInflater inflater;
public MenuAdapter(Context context, int resource, RealmList<DetailPerson> objects) {
super(context, resource, objects);
this.context = context;
this.persons = objects;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return persons.size();
}
private class ViewHolder {
TextView menuItem;
CheckBox checkBoxMenu;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_menu, null); //there its the error come from.
holder = new ViewHolder();
holder.menuItem = (TextView) convertView.findViewById(R.id.textViewMenuItem);
holder.checkBoxMenu = (CheckBox) convertView.findViewById(R.id.checkBoxMenu);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.menuItem.setText(" (" + persons.get(position).getPersonID() + ")");
holder.checkBoxMenu.setText(persons.get(position).getPersonName());
holder.checkBoxMenu.setChecked(false);
holder.checkBoxMenu.setTag(persons);
return convertView;
}
}
答案 0 :(得分:1)
您缺少初始化inflater
对象,您可以并且应该在MenuAdapter
构造函数中执行此操作,例如
public MenuAdapter(Context context, int resource, RealmList<DetailPerson> objects) {
super(context, resource, objects);
this.context = context;
this.persons = objects;
inflater = LayoutInflater.from(context);
}