android.location.Location类未定义无参数构造函数 [英] Class android.location.Location does not define a no-argument constructor

查看:159
本文介绍了android.location.Location类未定义无参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图简单地将具有两个变量(字符串和位置)的类推入Firebase并读取错误.

I've been trying to simply push and read a class with two variables, a String and a Location, to firebase and I've been getting this error.

**com.google.firebase.database.DatabaseException: Class android.location.Location does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
                                                                            at com.google.android.gms.internal.zzeas.zze(Unknown Source:51)
                                                                            at com.google.android.gms.internal.zzear.zzb(Unknown Source:772)
                                                                            at com.google.android.gms.internal.zzear.zza(Unknown Source:312)
                                                                            at com.google.android.gms.internal.zzear.zzb(Unknown Source:0)
                                                                            at com.google.android.gms.internal.zzeas.zze(Unknown Source:209)
                                                                            at com.google.android.gms.internal.zzear.zzb(Unknown Source:772)
                                                                            at com.google.android.gms.internal.zzear.zza(Unknown Source:0)
                                                                            at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
                                                                            at com.example.vish.mcapp.Selection$2$1.onDataChange(Selection.java:85)
                                                                            at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
                                                                            at com.google.android.gms.internal.zzduz.zza(Unknown Source:13)
                                                                            at com.google.android.gms.internal.zzdwu.zzbvb(Unknown Source:2)
                                                                            at com.google.android.gms.internal.zzdxa.run(Unknown Source:65)
                                                                            at android.os.Handler.handleCallback(Handler.java:790)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)**

我正在尝试从Firebase推送和读取其对象的类如下.

The class whose object I'm trying to push and read from firebase is below.

public class DeviceDetails {
   String nickname;
   Location location = new Location("Test");

    DeviceDetails()
     {
     }

     DeviceDetails(String nickname)
     {
     this.nickname = nickname;
     location.setLatitude(0.0);
     location.setLongitude(0.0);
     location.setTime(new Date().getTime());
     }

     DeviceDetails(String nickname, Location l)
     {
       this.nickname = nickname;
       location=l;
}

}

将数据推送到Firebase中就可以了.但是,如果没有遇到以上错误,我将无法阅读.

Pushing data into the Firebase works just fine. But I can't read it without running into the error above.

我为写入Firebase而编写的代码是:-

Code I've written for writing into firebase is : -

register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(TextUtils.isEmpty(nickname.getText().toString()))
            {
                Toast.makeText(Selection.this, "Nickname can't be empty", Toast.LENGTH_SHORT).show();
                return;
            }
            else
            {
                Location loc = new Location("");
                loc.setLatitude(19.0);
                loc.setLongitude(29.5);
                loc.setTime(new Date().getTime());
                DeviceDetails newDevice = new DeviceDetails(nickname.getText().toString(), loc);
                mDatabase.child("Phones").child(nickname.getText().toString().trim().replaceAll("\\s","")).setValue(newDevice);
            }


        }
    });

我编写的要从Firebase读取的代码,我认为是罪魁祸首是:-

Code I've written for reading from the Firebase and which I believe is the culprit is: -

retrieve.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            DatabaseReference database = FirebaseDatabase.getInstance().getReference();
            DatabaseReference myRef = database.child("Phones");
            myRef.child(nickToRetrieve.getText().toString().trim().replaceAll("\\s", "")).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    DeviceDetails dev = dataSnapshot.getValue(DeviceDetails.class);
                    double latitude = dev.location.getLatitude();
                    double longitude = dev.location.getLongitude();
                    String loc = "Latitude - " + latitude + " Longitude - " + longitude;
                    Toast.makeText(Selection.this, loc, Toast.LENGTH_LONG).show();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

任何帮助将不胜感激.

Any help is greatly appreciated.

推荐答案

当Firebase Realtime Database SDK反序列化来自数据库的对象时,它要求所有使用中的对象都具有可用于实例化的公共无参数构造函数.物体.通过使用setter方法或直接访问公共成员来设置对象中的字段.

When the Firebase Realtime Database SDK deserializes objects coming from the database, it requires that any objects in use have a public no-argument constructor that it can use to instantiate the object. Fields in the objects are set by using setter methods or direct access to public members.

Android的Location对象没有公共的无参数构造函数,因此SDK并不真正知道如何创建其实例.实际上,大多数序列化库将具有相同的要求.因此,不要使用Android的Location对象,而要使用自己的对象,并根据需要将数据复制进出.

Android's Location object dosen't have a public no-arg constructor, so the SDK doesn't really know how to create an instance of it. In fact, most serialization libraries will have the same requirement. So, instead of using Android's Location object, use one of your own, copying data in and out of it as needed.

这篇关于android.location.Location类未定义无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆