Android - 在firebase数据库中杀死一个孩子会导致我的应用程序死亡

时间:2017-05-31 15:04:49

标签: android firebase firebase-realtime-database

在我的应用中,我使用此ConnectionInformation.java存储每个用户的数据列表。

package trickandroid.fulllogin;

/**
 * Created by Simon Peter on 19-May-17.
 */

public class ConnectionInformation {

    String due, selectArea, connectionNum, doorNum, conName, phoneNum, aadharNum, rationNum;

    public  ConnectionInformation(){

    }

    public ConnectionInformation(String due, String selectArea, String connectionNum, String doorNum, String conName, String phoneNum, String aadharNum, String rationNum) {
        this.connectionNum = connectionNum;
        this.doorNum = doorNum;
        this.conName = conName;
        this.phoneNum = phoneNum;
        this.aadharNum = aadharNum;
        this.rationNum = rationNum;
        this.selectArea = selectArea;
        this.due = due;
    }

    public String getDue() {
        return due;
    }

    public void setDue(String due) {
        this.due = due;
    }

    public String getSelectArea() {
        return selectArea;
    }

    public void setSelectArea(String selectArea) {
        this.selectArea = selectArea;
    }

    public String getConnectionNum() {
        return connectionNum;
    }

    public void setConnectionNum(String connectionNum) {
        this.connectionNum = connectionNum;
    }

    public String getDoorNum() {
        return doorNum;
    }

    public void setDoorNum(String doorNum) {
        this.doorNum = doorNum;
    }

    public String getConName() {
        return conName;
    }

    public void setConName(String conName) {
        this.conName = conName;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getAadharNum() {
        return aadharNum;
    }

    public void setAadharNum(String aadharNum) {
        this.aadharNum = aadharNum;
    }

    public String getRationNum() {
        return rationNum;
    }

    public void setRationNum(String rationNum) {
        this.rationNum = rationNum;
    }
}

向firebase添加数据非常正常。这是我的数据库结构。 enter image description here

这是我的showData()方法,其中.getDoorNum()被调用以在ListView上显示它。

private void showData(DataSnapshot dataSnapshot) {
        if (expandableListView.getVisibility()==View.VISIBLE) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                ConnectionInformation cInfo = new ConnectionInformation();
                cInfo.setDoorNum(ds.child(userID).child("Connection Detail").child(areaName).child(key).getValue(ConnectionInformation.class).getDoorNum());
                cInfo.setPhoneNum(ds.child(userID).child("Connection Detail").child(areaName).child(key).getValue(ConnectionInformation.class).getPhoneNum());
                cInfo.setAadharNum(ds.child(userID).child("Connection Detail").child(areaName).child(key).getValue(ConnectionInformation.class).getAadharNum());
                cInfo.setRationNum(ds.child(userID).child("Connection Detail").child(areaName).child(key).getValue(ConnectionInformation.class).getRationNum());

                ArrayList<String> users = new ArrayList<>();
                users.add(cInfo.getDoorNum());
                users.add(cInfo.getPhoneNum());
                users.add(cInfo.getAadharNum());
                users.add(cInfo.getRationNum());

                ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, users);
                expandableListView.setAdapter(adapter);
            }
        }
    }

我使用此代码删除孩子。

disBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdata.child(oldArea).child(key).removeValue();
                connection.child(oldArea).child(key).removeValue();
                toast("Connection Number " + oldNum + " Disconnected Successfully");
                startActivity(new Intent(DisconnectActivity.this,AreaGridActivity.class));
            }
        });

代码在数据库中工作正常但我的应用程序因此错误而崩溃...

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String trickandroid.fulllogin.ConnectionInformation.getDoorNum()' on a null object reference
                                                                          at trickandroid.fulllogin.DetailsActivity.showData(DetailsActivity.java:449)
                                                                          at trickandroid.fulllogin.DetailsActivity.access$100(DetailsActivity.java:33)
                                                                          at trickandroid.fulllogin.DetailsActivity$3.onDataChange(DetailsActivity.java:129)
                                                                          at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                                          at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                                          at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                                          at android.os.Handler.handleCallback(Handler.java:751)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6236)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

https://gist.github.com/SimonPeter1909/d70be328dd6d9b099a8a96454922432f DetailsActivity.java

我尝试了很多方法,但我无法通过错误......

2 个答案:

答案 0 :(得分:1)

这里的情况是你有一个ValueEventListener,它在添加监听器时触发一次,然后在数据发生变化时再触发一次。听众明确希望找到具有特定areaNamekey的条目。

第一次监听器运行时这很好,但是点击按钮后你删除了areaNamekey的条目。这会再次触发侦听器(您更改了数据),侦听器尝试对该areaNamekey的条目执行某些操作,但它不存在,您将获得NullPointerException

有许多方法可以避免这种情况,但您可能需要重新考虑流程以及您想要发生的事情。

这里的快速解决方法可能是在尝试使用之前尝试检查条目是否存在:

if(ds.child(userID).child("Connection Detail").child(areaName).child(key).exists()) {
    ConnectionInformation cInfo = new ConnectionInformation();
    cInfo.setDoorNum(ds.child(userID).child("Connection Detail").child(areaName).child(key).getValue(ConnectionInformation.class).getDoorNum());
    cInfo.setPhoneNum(ds.child(userID).child("Connection Detail").child(areaName).child(key).getValue(ConnectionInformation.class).getPhoneNum());
...
...
} else {
//that entry doesn't exist, do something else
}

答案 1 :(得分:0)

没有足够的信息,但我认为你是callig getDoorNum,当时没有字符串。试试

if (!yourmodelclassobject.getDoorNum()==null)
{
//then print your value or whatever is that you do}

或者请发布更多信息