【问题标题】:Firebase Database Exception [duplicate]Firebase 数据库异常 [重复]
【发布时间】:2017-04-15 03:07:19
【问题描述】:

这是我打算从 firebase 数据库中读取位置类型数据项并将其存储在位置类型对象中的类。我什至尝试将数据快照存储在构造函数中具有 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();
    }
}

【问题讨论】:

  • 您需要在此处为​​您的代码示例添加一些上下文。在我看来,您需要阅读有关 Location 类的文档。
  • Class android.location.Location is missing a constructor with no arguments。看起来您不能在 Firebase 数据库中使用该类,然后
  • 很酷吗@ToothlessRebel
  • 我遇到了同样的问题,而我试图将数据快照存储在参数中具有位置类型变量的对象中。@Too
  • 在与 Firebase 数据库交互时,您不能使用任意 Java/Android 类。它不知道如何处理它们,而且这些类通常不遵循 Firebase 的规则(公共字段/getter,无参数公共构造函数)。因此,您必须创建自己的类来捕获位置的相关信息。见stackoverflow.com/questions/41045025/…

标签: android firebase firebase-realtime-database


【解决方案1】:

您的 Location 类需要有一个空的构造函数。

public Location () {}

【讨论】:

  • 已经这样做了。但没有帮助。
  • Android 中还有一个类叫做“Location”。见developer.android.com/reference/android/location/Location.html。确保您导入了正确的类。如果您将课程重命名为其他名称会更好。
  • 不,我没有名为“Location”的类,我编辑了您所指的导入的 Location 类
  • 您也可以在这里发布您的课程吗?您是如何编辑 Location 类的?
  • 好的@tingyik90
【解决方案2】:

您应该需要添加位置类的构造函数。

 public Location() {

}

【讨论】:

  • 已经这样做了。没用
  • @Override protected Location parseSnapshot(DataSnapshot snapshot) { return snapshot; } 试试这个
  • 试试什么? @ArslanJaved
猜你喜欢
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2018-06-05
  • 1970-01-01
相关资源
最近更新 更多