我的共享首选项存储列表视图的数据并检索它不起作用

Posted

技术标签:

【中文标题】我的共享首选项存储列表视图的数据并检索它不起作用【英文标题】:my shared pref to store data of list view and retrieve it back doesn't work 【发布时间】:2017-02-15 11:11:51 【问题描述】:

我使用共享首选项来存储我的灯数组列表,因为我使用这些数据将其放入带有自定义数组适配器的列表视图中,但每次它在我将数据转换为 json 以保存在共享首选项中的行时崩溃

String json1 = gson.toJson(lamps,type);

我不知道为什么请如果有人知道如何解决它 在此先感谢

  protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_light);
    Bundle extras = getIntent().getExtras();
    String name = null;
    if (extras != null) 
        name = extras.getString("name");
    
    ArrayList<lamp> lamps = new ArrayList<lamp>();
    SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
    Gson gson = new Gson();
    SharedPreferences.Editor prefsEditor = prefs.edit();

    String json = prefs.getString("lamp", null);
    ArrayList<lamp> arraylist= new ArrayList<>();
    if (json!=null) 
        Type type = new TypeToken<ArrayList<lamp>>() 
        .getType();
        arraylist = gson.fromJson(json, type);
        lamps.addAll(arraylist);
    
    if(extras!=null) 
        lamps.add(new lamp(name));
    
    LampAdapter adapter = new LampAdapter(this, lamps);
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);

    if(!lamps.isEmpty()) 
        Toast.makeText(this, "in", Toast.LENGTH_SHORT).show();
        Type type = new TypeToken<ArrayList<lamp>>() 
        .getType();
        String json1 = gson.toJson(lamps,type);
        prefsEditor.putString("lamp", json1);
        prefsEditor.commit();
    

Logcat 它在 light.java:53 中的第 53 行是这个代码 String json1 = gson.toJson(lamps,type);

E/androidRuntime: FATAL EXCEPTION: main
              Process: com.example.android.mechantus, PID: 2438
              java.lang.AssertionError: Missing field in com.google.android.gms.internal.zzajy$zza
                  at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(TypeAdapters.java:812)
                  at com.google.gson.internal.bind.TypeAdapters$30.create(TypeAdapters.java:838)
                  at com.google.gson.Gson.getAdapter(Gson.java:416)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96)
                  at com.google.gson.Gson.getAdapter(Gson.java:416)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96)
                  at com.google.gson.Gson.getAdapter(Gson.java:416)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96)
                  at com.google.gson.Gson.getAdapter(Gson.java:416)
                  at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
                  at com.google.gson.Gson.getAdapter(Gson.java:416)
                  at com.google.gson.Gson.toJson(Gson.java:653)
                  at com.google.gson.Gson.toJson(Gson.java:640)
                  at com.google.gson.Gson.toJson(Gson.java:595)
                  at com.example.android.mechantus.light.onCreate(light.java:53)
                  at android.app.Activity.performCreate(Activity.java:6237)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: java.lang.NoSuchFieldException: DEBUG
                  at java.lang.Class.getField(Class.java:891)
                  at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(TypeAdapters.java:801)
                  at com.google.gson.internal.bind.TypeAdapters$30.create(TypeAdapters.java:838) 
                  at com.google.gson.Gson.getAdapter(Gson.java:416) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96) 
                  at com.google.gson.Gson.getAdapter(Gson.java:416) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96) 
                  at com.google.gson.Gson.getAdapter(Gson.java:416) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160) 
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96) 
                  at com.google.gson.Gson.getAdapter(Gson.java:416) 
                  at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53) 
                  at com.google.gson.Gson.getAdapter(Gson.java:416) 
                  at com.google.gson.Gson.toJson(Gson.java:653) 
                  at com.google.gson.Gson.toJson(Gson.java:640) 
                  at com.google.gson.Gson.toJson(Gson.java:595) 
                  at com.example.android.mechantus.light.onCreate(light.java:53) 
                  at android.app.Activity.performCreate(Activity.java:6237) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

灯类

    package com.example.android.mechantus;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class lamp 

    String name;

    public lamp( String name) 

        this.name = name;
    
    public String getName() 
        return name;
    
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Lamps");
    public void on() 

        myRef.child(name).setValue(1);
    
    public void off() 

        myRef.child(name).setValue(0);
    


    

构建.gradle

apply plugin: 'com.android.application'

android 
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig 
        applicationId "com.example.android.mechantus"
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
        debug
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    

dependencies 
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
        exclude group: 'com.android.support', module: 'support-annotations'
    )
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    testCompile 'junit:junit:4.12'
    compile 'com.google.code.gson:gson:2.6.2'
    apply plugin: 'com.google.gms.google-services'
    compile 'com.google.firebase:firebase-database:9.2.1'

【问题讨论】:

请发布 Logcat。 @AsifPatel 我已经更新了问题,现在你可以找到 logcat 非常感谢 :) AssertionError in Gson EnumTypeAdapter when using Proguard Obfuscation的可能重复 【参考方案1】:
buildTypes 
    debug
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    

将此行添加到 build.gradle 文件并学习 proguard

【讨论】:

我已经在 build.gradle 中添加了这些行,但我不知道学习 proguard 是什么意思,但它仍然在同一行崩溃 你能分享你的gradle文件和灯类 我已经更新了问题,提供了你想要的信息@shaikhmanzoor gradle文件没有问题,bug在逻辑端 @Mohamed 为您的数据库和 myRef 对象添加瞬态关键字,即使这些对象像“瞬态 FirebaseDatabase 数据库”一样瞬态,并尝试我建议不要在您的 bean 类中编写任何业务逻辑,而是创建其他帮助类将处理您的 FireBase 工作

以上是关于我的共享首选项存储列表视图的数据并检索它不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Sharedpreferences 删除不起作用

是否有任何解决方案可以从共享首选项中存储和检索对象列表?

将 JWT 令牌存储在共享首选项中?并在整个应用程序中检索价值?

在共享首选项中存储和检索类对象

如何将文件共享首选项值保存到firebase并检索它

无法使用核心数据检索数据