2017-04-14 155 views
-1

这里就是我打算读火力数据库的位置类型的数据项,并将其存储在一个位置类型对象的类。我甚至尝试将数据快照存储在构造函数中具有位置类型参数的类的对象中,我得到的错误说明如下:火力地堡数据库异常

“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(); 
    } 
} 
+0

您需要在此代码示例中添加一些上下文。不过,在我看来,您需要阅读Location类的文档。 – ToothlessRebel

+0

'类android.location.Location缺少一个没有参数的构造函数。不像你可以使用这个类在你的火力地堡的数据库,然后 – njzk2

+0

这很酷@ToothlessRebel –

回答

1

您需要为Location类提供一个空的构造函数。

public Location() {} 
+0

已经做到了。但没有帮助。 –

+0

Android中还有一个名为“Location”的类。请参阅https://developer.android.com/reference/android/location/Location.html。确保您导入了正确的课程。如果您将课程重新命名为其他内容,会更好。 – tingyik90

+0

不,我没有名为“位置”的课程,我编辑了您引用的导入的位置类 –

0

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

public Location() { 

} 
+0

难道已经。没有工作 –

+0

@覆盖 保护的位置parseSnapshot(DataSnapshot快照){ 返回快照; } 试试这个 –

+0

尝试做什么? @ArslanJaved –