我正在尝试建立一个联系人的会议室数据库,以了解是否可以将他们的对话保存在应用程序中以保存SMS对话,但是该数据库不起作用:当我想插入联系人时,应用程序错误和logcat告诉我 :

2019-08-21 16:48:34.865 16177-16215/com.galinette.enregistreursms E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
    Process: com.galinette.enregistreursms, PID: 16177
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.galinette.enregistreursms.database.contactDao.insertContact(com.galinette.enregistreursms.model.Contact)' on a null object reference
        at com.galinette.enregistreursms.database.contactRepository.insertContact(contactRepository.java:32)
        at com.galinette.enregistreursms.database.contactRepository.putAllContactFirstTime(contactRepository.java:60)



所以为什么 ?

我确定我的方法Contact.getContacts()是因为我制作了一种将每个联系人的描述放入文本文件的方法,并且可以正常工作...

一,联系方式:

@Entity (tableName = "Contact")
public class Contact {
    @PrimaryKey (autoGenerate = true)
    private long id;
    private String phoneNumber;
    private String contactName;
    private boolean isSavable;

    public Contact(String contactName, String phoneNumber) {
        this.phoneNumber = phoneNumber;
        this.contactName = contactName;
        this.isSavable = false;
    }
 public static List<Contact> getContacts(Context ctx) {
        List<Contact> list = new ArrayList<>();
        ContentResolver contentResolver = ctx.getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                    InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(),
                            ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));

                    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id));
                    Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

                    Bitmap photo = null;
                    if (inputStream != null) {
                        photo = BitmapFactory.decodeStream(inputStream);
                    }
                    while (cursorInfo.moveToNext()) {
                        Contact info = new Contact(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)),
                                cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) );
                       // info.photo = photo;
                       // info.photoURI= pURI;
                        list.add(info);
                    }

                    cursorInfo.close();
                }
            }
            cursor.close();
        }
        return list;
    }



道:

@Dao
public interface contactDao {
    @Insert
    public void insertContact(Contact contact);

    @Query("SELECT * FROM Contact")
    LiveData<List<Contact>> getAllContacts();

    @Update
    void updateContact(Contact contact);

    @Delete
    void deleteContact(Contact contact);
}



和contactrepository:

public class contactRepository {


    private contactDatabase db;
    private contactDao contactDao;


    public contactRepository (Context context){
        db = contactDatabase.getInstance(context);
    }

    public void insertContact (Contact contact) {
        contactDao.insertContact(contact);
    }

    public void updateContact (Contact  contact) {
        db.contactDao().updateContact(contact);
    }

    public LiveData<List<Contact>> getAllContacts(){
        return contactDao.getAllContacts();
    }

    public void deleteContact (Contact contact) {
        db.contactDao().deleteContact(contact);
    }

    public void updateAllContacts (List <Contact> contactList){
        for(int i = 0; i<=contactList.size(); i++){
            db.contactDao().insertContact(contactList.get(i));
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void putAllContactFirstTime(){
        List<Contact> contactList = Contact.getContacts(EnregistreurSMS.getAppContext());
        for(int i = 0; i<contactList.size();i++) {
            this.insertContact(contactList.get(i));
        }



}


...

我只希望道工作...

谢谢你的帮助 !

最佳答案

从您提供的代码来看,contactDao中的contactRepository似乎尚未实例化。除非您调用类似contactDao = new ContactDao()的名称,否则它将为null。

08-04 06:02