我正在使用FirebaseListAdapter
填充我的AutoCompleteTextView
。我想根据firebase上存储的GeoPoints
返回数据库。密钥显示在Logcat中,但应用程序崩溃。
Logcat崩溃:
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.put(java.lang.Object, java.lang.Object)' on a null object reference
at com.android.avad.adapters.FirebaseListAdapter.addSingle(FirebaseListAdapter.java:197)
at com.android.avad.fragments.SearchFragment$5$1.onDataChange(SearchFragment.java:480)
显示值的Logcat:
10-24 14:17:55.574 9952-9952/com.android.avad D/FirebaseApp: Notifying background state change listeners.
10-24 14:17:55.877 9952-9952/com.android.avad I/SearchFragment: Contact permissions have already been granted. Displaying contact details.
10-24 14:17:55.950 9952-9952/com.android.avad D/SearchFragment: Key Sameer entered the search area at [15.5425561,73.8210879]
10-24 14:17:55.951 9952-9952/com.android.avad D/SearchFragment: item added Sameer
10-24 14:17:55.962 9952-9952/com.android.avad D/SearchFragment: item updated: Sameer
10-24 14:17:55.985 9952-9952/com.android.avad D/SearchFragment: Key Sameer entered the search area at [15.5425561,73.8210879]
10-24 14:17:55.986 9952-9952/com.android.avad D/SearchFragment: item added Sameer
10-24 14:17:55.987 9952-9952/com.android.avad D/SearchFragment: item updated: Sameer
10-24 14:17:56.492 9952-9952/com.android.avad D/SearchFragment: Key Sameer entered the search area at [15.5425561,73.8210879]
10-24 14:17:56.494 9952-9952/com.android.avad D/SearchFragment: item added Sameer
10-24 14:17:56.495 9952-9952/com.android.avad D/SearchFragment: item updated: Sameer
10-24 14:17:58.742 9952-9952/com.android.avad D/FirebaseListAdapter: filter for S, results nr: 16
10-24 14:17:58.962 9952-9952/com.android.avad D/FirebaseListAdapter: filter for Sa, results nr: 16
10-24 14:18:02.063 9952-9952/com.android.avad D/SearchFragment: Key Sameer entered the search area at [15.5425561,73.8210879]
10-24 14:18:02.079 9952-9952/com.android.avad D/SearchFragment: item added Sameer
10-24 14:18:02.104 9952-9952/com.android.avad D/AndroidRuntime: Shutting down VM
这是我用于查询的代码 -
mDatabase = FirebaseDatabase.getInstance().getReference("Interests");
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("GeoPoints");
geoFire = new GeoFire(ref);
mItemListAdapter = new ItemListAdapter(mDatabase.equalTo("GeoPoints"), R.layout.auto_complete_items, getActivity());
private void startGeoQuery(){
query = geoFire.queryAtLocation(center, 1);
query.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Log.d(TAG, "Key " + key + " entered the search area at [" + location.latitude + "," + location.longitude + "]");
DatabaseReference tempRef = mDatabase.child(key);
tempRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// I add the deal only if it doesn't exist already in the adapter
String key = snapshot.getKey();
if (!mItemListAdapter.exists(key)) {
Log.d(TAG, "item added " + key);
mItemListAdapter.addSingle(snapshot);
mItemListAdapter.notifyDataSetChanged();
} else {
//...otherwise I will update the record
Log.d(TAG, "item updated: " + key);
mItemListAdapter.update(snapshot, key);
mItemListAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "cancelled with error:" + databaseError.getMessage());
}
});
}
这是我的ItemListAdapter
扩展FirebaseListAdapter
-
public ItemListAdapter(Query ref , int layout, Activity activity) {
super(ref,Search.class,layout,activity);
}
/**
* Bind an instance of the ExampleObject class to our view. This method is called by <code>FirebaseListAdapter</code>
* when there is a data change, and we are given an instance of a View that corresponds to the layout that we passed
* to the constructor, as well as a single ExampleObject instance that represents the current data to bind.
*
* @param v A view instance corresponding to the layout we passed to the constructor.
* @param model An instance representing the current state of a message
*/
@Override
protected void populateView(View v, Search model) {
TextView body = (TextView)v.findViewById(R.id.auto_text);
// populate the list element
body.setText(model.getBody());
}
@Override
protected List<Search> filters(List<Search> models, CharSequence constraint) {
List<Search> filterList = new ArrayList<>();
for (int i = 0; i < models.size(); i++) {
/* implement your own filtering logic
* and then call filterList.add(models.get(i));
*/
filterList.add(models.get(i));
}
return filterList;
}
@Override
protected Map<String, Search> filterKeys(List<Search> mModels) {
return null;
}
}
答案 0 :(得分:0)
reference=FirebaseDatabase.getInstance().getReference();
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter <String> adp;
uname=(AutoCompleteTextView) findViewById(R.id.uname);
adp = new ArrayAdapter<>(this,android.R.layout.select_dialog_item,list);
uname.setThreshold(1);
uname.setAdapter(adp);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dsp : dataSnapshot.getChildren())
{
if(dsp!= null){
for (DataSnapshot dsp1 : dsp.getChildren()){
list.add(String.valueOf(dsp1.getKey()));
adp.notifyDataSetChanged();
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});