我在寻找一种方法来存储我的应用程序创建的地理围栏时遇到问题,以便在我再次运行该应用程序时加载它们。目的是显示用户创建的地理围栏列表。
我尝试使用Java的序列化对它们进行序列化,也尝试使用Gson将它们转换为Json字符串。 Gson的问题是我尝试加载它们时,它给我一个错误消息,指出Geofence不是一个类。 (是的,这是一个接口)。

先感谢您。

PS:这是我班上的一些代码

public class Manager implements ResultCallback<Status>, LocationListener {

// Tag para fazer debug
protected static final String MAN = "Manager";
protected static final String SMS = "eventSMS";
protected static final String RING = "eventRing";

private Context mAppContext;
private GoogleApiClient mGoogleApiClient;

private LocationRequest mLocationRequest;

// Listas de Geofences

private TreeMap<String, Geofence> mGeofences;
private TreeMap<String, Event> mEvents;
private int mCounterActiveGeofences;

private PendingIntent mGeofencePendingIntent;

private Intent updateLocIntent;

/* Save  */
private Gson gson;
private SharedPreferences prefs;

public Manager(Context context, GoogleApiClient googleApiClient){

    mGeofences = new TreeMap<String, Geofence>();
    mEvents = new TreeMap<String, Event>();
    mGeofencePendingIntent = null;
    mAppContext = context;
    mGoogleApiClient = googleApiClient;
    mCounterActiveGeofences = 0;
    prefs = context.getSharedPreferences(Constants.Geofences, Context.MODE_PRIVATE);
    gson = new Gson();
}

...

public void saveGeofences(){
    String json;
    Geofence geofence;
    SharedPreferences.Editor editor = prefs.edit();
    Log.d(MAN, "Saving Geofences...");
    for (Map.Entry<String, Geofence> entry : mGeofences.entrySet()) {
        geofence = mGeofences.get(entry.getKey());
        json = gson.toJson(geofence);
        editor.putString(geofence.getRequestId(), json);
        Log.d(MAN, geofence.getRequestId() + " json: " + json);
    }
    editor.apply();
}

public void loadGeofences(){
    Map<String, ?> keys = prefs.getAll();
    Log.d(MAN, "Loading Geofences...");
    for (Map.Entry<String, ?> entry : keys.entrySet()) {
        String jsonString = prefs.getString(entry.getKey(), null);
        Geofence geofence = gson.fromJson(jsonString, Geofence.class);
        Log.d(MAN, geofence.getRequestId());
        mGeofences.put(geofence.getRequestId(), geofence);
    }
}
}


这是LogCat:

09-09 21:37:16.148  18137-18137/? D/dalvikvm﹕ Late-enabling CheckJNI
09-09 21:37:17.515  18137-18137/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
09-09 21:37:17.515  18137-18137/? W/dalvikvm﹕ VFY: unable to resolve virtual method 614: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
09-09 21:37:17.515  18137-18137/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-09 21:37:17.515  18137-18137/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
09-09 21:37:17.515  18137-18137/? W/dalvikvm﹕ VFY: unable to resolve virtual method 636: Landroid/content/res/TypedArray;.getType (I)I
09-09 21:37:17.515  18137-18137/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-09 21:37:18.133  18137-18137/? D/GoogleAPIMsg﹕ Construcao do Client Google
09-09 21:37:18.164  18137-18137/? D/Manager﹕ Loading Geofences...
09-09 21:37:18.203  18137-18137/? D/AndroidRuntime﹕ Shutting down VM
09-09 21:37:18.203  18137-18137/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x419307c0)
09-09 21:37:18.234  18137-18137/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartlocation.wilcocsjrxxlxpto/com.smartlocation.wilcocsjrxxlxpto.HomeScreen}: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.google.android.gms.location.Geofence. Register an InstanceCreator with Gson for this type may fix this problem.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
            at android.app.ActivityThread.access$600(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.google.android.gms.location.Geofence. Register an InstanceCreator with Gson for this type may fix this problem.
            at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:210)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
            at com.google.gson.Gson.fromJson(Gson.java:810)
            at com.google.gson.Gson.fromJson(Gson.java:775)
            at com.google.gson.Gson.fromJson(Gson.java:724)
            at com.google.gson.Gson.fromJson(Gson.java:696)
            at com.smartlocation.wilcocsjrxxlxpto.core.Manager.loadGeofences(Manager.java:203)
            at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.load(HomeScreen.java:252)
            at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.onCreate(HomeScreen.java:66)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
            at android.app.ActivityThread.access$600(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
            at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:207)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
            at com.google.gson.Gson.fromJson(Gson.java:810)
            at com.google.gson.Gson.fromJson(Gson.java:775)
            at com.google.gson.Gson.fromJson(Gson.java:724)
            at com.google.gson.Gson.fromJson(Gson.java:696)
            at com.smartlocation.wilcocsjrxxlxpto.core.Manager.loadGeofences(Manager.java:203)
            at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.load(HomeScreen.java:252)
            at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.onCreate(HomeScreen.java:66)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
            at android.app.ActivityThread.access$600(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.InstantiationException: can't instantiate class com.google.android.gms.location.Geofence; abstract class or interface
            at sun.misc.Unsafe.allocateInstance(Native Method)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
            at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:207)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
            at com.google.gson.Gson.fromJson(Gson.java:810)
            at com.google.gson.Gson.fromJson(Gson.java:775)
            at com.google.gson.Gson.fromJson(Gson.java:724)
            at com.google.gson.Gson.fromJson(Gson.java:696)
            at com.smartlocation.wilcocsjrxxlxpto.core.Manager.loadGeofences(Manager.java:203)
            at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.load(HomeScreen.java:252)
            at com.smartlocation.wilcocsjrxxlxpto.HomeScreen.onCreate(HomeScreen.java:66)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
            at android.app.ActivityThread.access$600(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
09-09 21:37:18.648  18137-18137/? I/Process﹕ Sending signal. PID: 18137 SIG: 9


该行Manager.java:203是“ Geofence geofence = gson.fromJson(jsonString,Geofence.class);”从课堂上。

通过尝试序列化地理围栏而生成的json字符串是:

Json: {"zzDK":"xxd","zzCY":1,"zzazj":-1,"zzaxB":39.2079714,"zzaxC":-8.9593288,"zzaxD":100.0,"zzaxE":0,"zzaxF":-1,"zzaxy":3,"zzaxA":1}

最佳答案

您不能直接这样做。您必须自己镜像地理围栏设置(纬度,经度,半径等)。

关于java - 如何在磁盘中存储地理围栏? (安卓),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32489043/

10-11 12:35