android - 名称为空 - 自定义 ArrayAdapter

标签 android firebase firebase-realtime-database

我有一个自定义的 ArrayAdapter,它应该显示用户的个人资料图像和用户名。

我一直在学习本教程:

Custom Android Layouts with Your Own ArrayAdapter

然而,当我打开我的 Activity 时,应用程序崩溃并显示:

    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 设置图像,您需要将图像 url 作为参数放入 load() 方法中,但您放置的是整数 ID。你可以试试这个

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

请提供您的 ContactsActivity.java 代码以作进一步说明。

关于android - 名称为空 - 自定义 ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53135646/

相关文章:

node.js - 允许用户仅在 firebase 上修改自己的数据

android - 如果在首选项中设置,则告诉 Glide 不要加载图像

android - 将 Bitmap 转换为资源 ID

java - 在手机上获取当前位置,不起作用? [安卓]

ios - 无法转换为预期参数类型“AuthDataResultCallback?”

Firebase RTDB 在关键字列表中搜索关键字

android - 如何使用 Firebase 数据库收集 child 的数值

java - 无法从 FacebookSDK 解析目标 'android-9'

firebase - Firebase事务的奇怪行为

firebase - 如果我删除文档但不删除其在 firestore 中的子集合,会发生什么情况?