我创建了以下名为联系人数据的类:
public class ContactData {
String contact_id;
String first_name;
String last_name;
String email;
Uri icon;
public ContactData(String contact_id,String first_name,String last_name,String email){
this.contact_id=contact_id;
this.first_name=first_name;
this.last_name=last_name;
this.email=email;
this.icon= Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,contact_id);
if (this.icon == null){
this.icon = Uri.parse("android.resource://com.project.iandwe/drawable/ic_contacts");
}
}
public String getContact_id() {
return contact_id;
}
public void setContact_id(String contact_id) {
this.contact_id = contact_id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Uri getIcon() {
if (this.icon == null){
this.icon = Uri.parse("android.resource://com.project.iandwe/drawable/ic_contacts");
}
return this.icon;
}
public void setIcon(Uri icon) {
this.icon = icon;
}
}
现在我尝试像这样创建这个类的对象:
package com.project.iandwe.Adaptor;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.project.iandwe.Data.ContactData;
import com.project.iandwe.R;
import java.util.ArrayList;
/**
* Created by NathanDrake on 6/4/2014.
*/
public class ContactSelectAdaptor extends BaseAdapter {
ArrayList<ContactData> listViewRows;
Context context;
public ContactSelectAdaptor(Context context){
//Resources resources = context.getResources();
this.context =context;
UserDatabaseAdapter userDatabaseAdapter = new UserDatabaseAdapter(context);
Cursor cursor = userDatabaseAdapter.getUserContacts();
while (cursor.moveToNext()){
// ContactData listViewRow = new ListViewRow();
String contact_id = cursor.getString(0);
String first_name = cursor.getString(1);
String last_name = cursor.getString(2);
String email = cursor.getString(3);
// String icon = cursor.getString(4);
Log.d("ContactSelectAdaptor"," " + contact_id + " " + first_name + " " + last_name+ " " + email);
if (last_name ==null){
last_name = " ";
}
listViewRows.add(new ContactData(contact_id,first_name,last_name,email));
}
}
@Override
public int getCount() {
return listViewRows.size();
}
@Override
public Object getItem(int position) {
return listViewRows.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.listview_contact_select,parent,false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageViewProfile);
TextView textViewName = (TextView) view.findViewById(R.id.textViewDisplayName);
TextView textViewEmail = (TextView) view.findViewById(R.id.textViewEmailAddress);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBoxSelected);
ContactData contactData = listViewRows.get(position);
imageView.setImageURI(contactData.getIcon());
textViewName.setText(contactData.getFirst_name());
textViewEmail.setText(contactData.getEmail());
checkBox.setChecked(false);
return view;
}
}
我添加了log.d来检查数据是否为null但是它正确地显示如下:
06-03 17:00:37.757 2392-2392/com.project.iandwe D/ContactSelectAdaptor﹕ 3 Batman null bat@gmail.com
我收到以下错误:
06-03 17:00:37.767 2392-2392/com.project.iandwe E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.project.iandwe, PID: 2392
java.lang.NullPointerException
at com.project.iandwe.Adaptor.ContactSelectAdaptor.<init>(ContactSelectAdaptor.java:49)
at com.project.iandwe.Menu.ContactsFragments.onActivityCreated(ContactsFragments.java:42)
错误发生在以下两行代码中:
listViewRows.add(new ContactData(contact_id,first_name,last_name,email));
这是在ContactSelectAdaptor类中 我在片段中通过此方法创建类对象:
listView.setAdapter(new ContactSelectAdaptor(getActivity()));
我认为错误发生的原因是由于last_name为null或者Uri被设置为null,但即使在这种情况下,ContactData也应该能够创建一个对象。
我正在使用上面的适配器类来填写自定义列表视图。请让我知道我在这里缺少什么
答案 0 :(得分:0)
它为null,因为您没有实例化listViewRows
。
<强>溶液强>
在添加到ArrayList
listViewRows = new ArrayList<ContactData>();
答案 1 :(得分:0)
listViewRows
被宣布:
ArrayList<ContactData> listViewRows;
...但是从未初始化,这将导致空指针。