這裏就是我打算讀火力數據庫的位置類型的數據項,並將其存儲在一個位置類型對象的類。我甚至嘗試將數據快照存儲在構造函數中具有位置類型參數的類的對象中,我得到的錯誤說明如下:火力地堡數據庫異常
「com.google.firebase.database.DatabaseException:Class android.location。位置缺少一個沒有參數的構造函數「
package com.example.aadi.sarthi_;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class ActiveNotifications extends Fragment{
public static final String NOTIFICATION_MSG = "NOTIFICATION MSG";
public RecyclerView recyclerView;
String user_mac;
public List<notifyListRowItem> result;
public UserAdapter userAdapter;
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private DatabaseReference eventRef = reference.child("Events");
// Create a Intent send by the notification
public static Intent makeNotificationIntent(Context context, String msg) {
Intent intent = new Intent(context, ActiveNotifications.class);
intent.putExtra(NOTIFICATION_MSG, msg);
return intent;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_active_notifications, container, false);
getMac();
result = new ArrayList<>();
recyclerView = (RecyclerView) view.findViewById(R.id.notification_list);
recyclerView.setHasFixedSize(true);
LinearLayoutManager rlm = new LinearLayoutManager(getActivity());
rlm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(rlm);
/*
createNotifyListRowItem();*/
userAdapter = new UserAdapter(result);
recyclerView.setAdapter(userAdapter);
updateList();
return view;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case 0:
break;
case 1:
break;
}
return super.onContextItemSelected(item);
}
public void updateList(){
final DatabaseReference locationRef = reference.child(String.valueOf(user_mac)).child("location");
final Location userLoc = null;
final Location eventLoc = null;
locationRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
Location location = dataSnapshot.getValue(Location.class);
userLoc.set(location);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
eventRef.limitToFirst(2).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
eventRef.push().child("location");
Location location = dataSnapshot.getValue(Location.class);
eventLoc.set(location);
if(eventLoc.distanceTo(userLoc)<=1000) {
result.add(dataSnapshot.getValue(notifyListRowItem.class));
userAdapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.w("asd", "In onChildChanged");
notifyListRowItem notifyListRowItem = dataSnapshot.getValue(notifyListRowItem.class);
int index = getItemIndex(notifyListRowItem);
result.set(index,notifyListRowItem);
userAdapter.notifyItemChanged(index);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.w("In ", "OnchildRemoved");
notifyListRowItem model = dataSnapshot.getValue(notifyListRowItem.class);
int index = getItemIndex(model);
result.remove(index);
userAdapter.notifyItemRemoved(index);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.w("IN", "onChildMoved");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("IN", "onChildCancelled");
return;
}
});
}
private int getItemIndex(notifyListRowItem item){
int index = -1;
for (int i=0;i<result.size();i++){
if(result.get(i).key.equals(item.key)){
index = i;
break;
}
}
return index;
}
protected void getMac(){
GettingMac gettingMac = new GettingMac(getActivity());
user_mac = gettingMac.Mac();
}
}
您需要在此代碼示例中添加一些上下文。不過,在我看來,您需要閱讀Location類的文檔。 – ToothlessRebel
'類android.location.Location缺少一個沒有參數的構造函數。不像你可以使用這個類在你的火力地堡的數據庫,然後 – njzk2
這很酷@ToothlessRebel –