Firebase 数据上传崩溃 Android 应用程序

Firebase data upload crashing Android application

我正在收集信号数据并将其上传到数据库。

我有几个功能并在尝试实现其余功能之前做了一些测试功能。当我上传我想使用的 classes 而不是测试用例时,它崩溃了。

这里是 classes 方法和初始化的代码:

初始化数据库

FirebaseDatabase database;
DatabaseReference myRef;

class是:

package com.example.mike.maps_example;

/**
 * Created by Mike on 3/10/2017.
 */

public class WcdmaSignalEntry {
    Double Latitude, Longitude;
    String dBm;
    String Cid;
    String Lac;
    String Mcc;
    String Mnc;
    String Psc;
    String Uarfcn;
    String connectionType = "WCDMA";

public WcdmaSignalEntry(){
    // Default constructor required for calls to DataSnapshot.getValue(User.class)

}

public WcdmaSignalEntry(double Latitude, double Longitude, String dBm, String Cid, String Lac, String Mcc, String Mnc, String Psc, String Uarfcn){
    this.Latitude = Latitude;
    this.Longitude = Longitude;
    this.dBm = dBm;
    this.Cid = Cid;
    this.Lac = Lac;
    this.Mcc = Mcc;
    this.Mnc = Mnc;
    this.Psc = Psc;
    this.Uarfcn = Uarfcn;
    }
}

这里是测试代码class:

package com.example.mike.maps_example;

/**
 * Created by Mike on 28/9/2017.
 */

public class CellSignalEntry {
    public double Latitude;
    public double Longitude;
    public String dBm;
    public String CI; //cell identity
    public String LAC; //Location Area Code
    public String PSC; //primary scrambling code
    public String TA; //timing advance
    public String PCI_LTE; //physical cell identifier for LTE
    public String PCI_GSM; //physical cell identifier for GSM

public CellSignalEntry() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public CellSignalEntry(String dBm, String CI, String LAC, String PSC, String TA, String PCI_LTE, String PCI_GSM, double Latitude, double Longitude) {
    this.dBm = dBm;
    this.CI = CI;
    this.LAC = LAC;
    this.PSC = PSC;
    this.TA = TA;
    this.PCI_GSM = PCI_GSM;
    this.PCI_LTE = PCI_LTE;
    this.Latitude = Latitude;
    this.Longitude = Longitude;
    }
}

我有两个方法。

private void writeNewWcdmaEntry(double lon, double lat,  String dBm, String Cid, String Lac,  String Mcc, String Mnc,String Psc, String Uarfcn, long entry){
    WcdmaSignalEntry newEntry = new WcdmaSignalEntry(lon, lat, dBm, Cid, Lac, Mcc, Mnc, Psc, Uarfcn);
    tvView.setText(newEntry.dBm);
    tvView.setText(newEntry.Cid);
    //myRef.child("Entry").child(Long.toString(entry)).setValue(newEntry);
}

private void writeNewUser(long userId, String dBm, String CI, String LAC, String PSC, String TA, String PCI_LTE, String PCI_GSM, double currentLat, double currentLon) {
    CellSignalEntry user = new CellSignalEntry(dBm, CI, LAC, PSC, TA, PCI_LTE, PCI_GSM, currentLat, currentLon);

    myRef.child("Entry").child(Long.toString(userId)).setValue(user);
}

我的应用程序 运行 在我 运行 第二种方法时很好,但是当我 运行 第一种方法时,如果我取消注释此行,它会在将数据上传到 firebase 数据库时崩溃:

//myRef.child("Entry").child(Long.toString(entry)).setValue(newEntry);

我完全被难住了。非常感谢任何帮助。

编辑:在发布这个之后我就明白了。我不知道为什么,但是在 WcdmaSignalEntry class 中设置所有变量 "public" 解决了这个问题,但我不确定为什么,我对 java 很陌生,所以解释会很好: )!

Java documentation 说:

If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

如果这两种方法(来自你的问题)不在 class WcdmaSignalEntry 所在的同一个包内,即在 com.example.mike.maps_example 内,那么你无法访问这些变量,除非它们是public。

有关 java 中 访问修饰符的更多信息,请参阅 this