在Android中将联系人号码与Firebase数据库电话号码进行比较时出错

时间:2019-03-22 11:20:55

标签: android firebase firebase-realtime-database contacts android-contacts

   **

我的应用程序暗恋     在Android中将联系人号码与Firebase数据库电话号码进行比较时出错       userlist.add(mUser);中的错误;在MainActivity中:     [在此处输入图片描述] [1]

**

E / AndroidRuntime:致命异常:主要

    java.lang.NullPointerException
        at com.example.mohaiminur.com.whatp.MainActivity$LoadContact$1.onDataChange(MainActivity.java:160)
        at com.google.firebase.database.zzp.onDataChange(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:800)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:194)
        at 

2 个答案:

答案 0 :(得分:1)

您永远不会在任何地方初始化userList。调用userlist.add(mUser);时会给出一个NullPointerException,因为您无法将用户添加到不存在的列表中。

要解决此问题,请在尝试将用户添加到列表之前创建列表。例如,在onCreate中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.contacts_list);
    recyclerView.setHasFixedSize(true);

    recyclerView.setLayoutManager(layoutManager);
    selectUsers = new ArrayList<Contacts>();
    showContacts();

    userList = new ArrayList<>();
}

答案 1 :(得分:0)

您的ArrayList未初始化,这就是您的应用程序崩溃的原因。首先初始化您的ArrayList。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) 
                findViewById(R.id.contacts_list);
    recyclerView.setHasFixedSize(true);

    recyclerView.setLayoutManager(layoutManager);
    selectUsers = new ArrayList<Contacts>();
    userlist = new ArrayList<Contacts>();
    showContacts();

}