这是我打算从firebase数据库中读取Location类型数据项并将其存储在Location类型Object中的类。我甚至试图将数据快照存储在构造函数中具有Location类型参数的类的对象中,我得到了同样的错误:
" com.google.firebase.database.DatabaseException:类android.location.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();
}
}
答案 0 :(得分:1)
您需要为Location类设置一个空构造函数。
public Location () {}
答案 1 :(得分:0)
您应该添加Location Class的构造函数。
public Location() {
}