我正在尝试使用firebase,但当尝试在listView中加载数据时,应用程序返回错误:
java.lang.RuntimeException:无法启动activity ComponentInfo {my activity} java.lang.NullPointerException:尝试对 空对象引用调用虚方法'void com.firebase.ui.database.FirebaseListAdapter.startListening ()‘。
如果有人能看到这个问题,那将是非常有帮助的。
我的数据库的方面:
location 40434734 latitude: 40.434734 longitude: -3.70701 txt: "test"
我的班级:
public class start extends AppCompatActivity{ ListView lv; FirebaseListAdapter adapter; @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); lv = (ListView) findViewById(R.id.listView); Query query = FirebaseDatabase.getInstance().getReference().child("location"); FirebaseListOptions<LocationData> options = new FirebaseListOptions.Builder<LocationData>() .setLayout(R.layout.list_location) .setQuery(query,LocationData.class) .build(); adapter = new FirebaseListAdapter(options) { @Override protected void populateView(View v, Object model, int position) { TextView stdID = v.findViewById(R.id.nombre); LocationData ld = (LocationData) model; stdID.setText(ld.getTxt().toString()); lv.setAdapter(adapter); @Override protected void onStart() { super.onStart(); adapter.startListening(); @Override protected void onStop() { super.onStop(); adapter.stopListening(); }
}
创建对象的LocationData类:
public class LocationData { double latitude; double longitude; String txt; public LocationData(){ public LocationData(double latitude, double longitude, String txt) { this.latitude = latitude; this.longitude = longitude; this.txt = txt; public String getTxt() { return txt; public void setTxt(String txt) { this.txt = txt; public double getLatitude() { return latitude; public void setLatitude(double latitude) { this.latitude = latitude; public double getLongitude() { return longitude; public void setLongitude(double longitude) {