名称为null-自定义数组适配器



我有一个自定义的ArrayAdapter,它应该显示用户的配置文件图像和用户名。

我一直在学习这个教程:

使用自己的阵列适配器自定义Android布局

然而,当我打开我的活动时,应用程序崩溃并说:

java.lang.NullPointerException: name is null

这是真的,因为我还没有建立一个功能供用户添加。相反,它应该显示一个占位符图像。(毕加索(

这是我的CustomAdapter:

public class ContactsAdapter extends ArrayAdapter<CustomContact>{
private Context context;
private List<CustomContact> contactList;
//constructor, call on creation
public ContactsAdapter(Context context, int resource, ArrayList<CustomContact> objects) {
super(context, resource, objects);
this.context = context;
this.contactList = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
//get the user we are displaying
CustomContact customcontact = contactList.get(position);
//get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylist, null);

ImageView image = (ImageView) view.findViewById(R.id.friend_icon2);
TextView friend_name = (TextView) view.findViewById(R.id.Itemname);
//get & set username
String completeUsername = customcontact.getUsername();
friend_name.setText(completeUsername);
//get the image associated with this user
int imageID = context.getResources().getIdentifier(customcontact.getImageURL(), "string", context.getPackageName());
//set image
Picasso.get().load(imageID).placeholder(R.drawable.placeholder_image).resize(96, 96)
.centerCrop().into(image);

return view;
}
}

返回null的问题行是定义ImageID:的行

//get the image associated with this user
int imageID = context.getResources().getIdentifier(customcontact.getImageURL(), "string", context.getPackageName());

这是我的ContactsActivity.java

public class ContactsActivity extends ListActivity {
private DatabaseReference mFriendDatabase;
private DatabaseReference mRootRef;
private DatabaseReference mUsersDatabase;
private FirebaseAuth mAuth;
private String mCurrent_user_id;
private View mMainView;
private RecyclerView mFriendsList;
private ListView contacts_list, lstView;
private ArrayAdapter dataAdapter;
private RelativeLayout individual_contact;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
ArrayList<CustomContact> contactList = new ArrayList<>();
ArrayAdapter<CustomContact> adapter = new ContactsAdapter(ContactsActivity.this, 0, contactList);
mAuth = FirebaseAuth.getInstance();
mRootRef = FirebaseDatabase.getInstance().getReference();
mCurrent_user_id = mAuth.getCurrentUser().getUid();
mFriendDatabase = FirebaseDatabase.getInstance().getReference().child("friends").child(mCurrent_user_id);
mFriendDatabase.keepSynced(true);
contacts_list = (ListView) findViewById(android.R.id.list);
contacts_list.setAdapter(adapter);
individual_contact = (RelativeLayout) findViewById(R.id.mylist);

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");



usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot friendKeySnapshot: dataSnapshot.getChildren()) {
String friendKey = friendKeySnapshot.getKey();

usersRef.child(friendKey).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot friendSnapshot) {
String friendName = friendSnapshot.child("username").getValue(String.class);
String friendPicture = friendSnapshot.child("image_url").getValue(String.class);
contactList.add(
new CustomContact(friendName, friendPicture)
);
contacts_list.setAdapter(adapter);
//add event listener so we can handle clicks
AdapterView.OnItemClickListener adapterViewListener = new AdapterView.OnItemClickListener() {
//on click
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CustomContact customContact = contactList.get(position);
Intent intent = new Intent(ContactsActivity.this, MessageActivity.class);
intent.putExtra("username", customContact.getUsername());
intent.putExtra("ImageURL", customContact.getImageURL());
startActivity(intent);
}
};
//set the listener to the list view
contacts_list.setOnItemClickListener(adapterViewListener);

}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}

要用Picasso设置图像,您需要在load((方法中放入一个图像url作为参数,但您放入的是一个整数id。您可以尝试这个

Picasso.get().load(customcontact.getImageURL()).placeholder(R.drawable.placeholder_image).resize(96, 96)
.centerCrop().into(image);

请提供您的ContactsActivity.java代码以进行进一步澄清。

最新更新