我共享pref来存储列表视图的数据并将其检索回来不起作用

时间:2016-10-06 15:52:26

标签: android listview arraylist sharedpreferences custom-adapter

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

String json1 = gson.toJson(lamp,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 it crsahes in light.java:53 at 53,这是这段代码 字符串json1 = gson.toJson(灯,类型);

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);
    }


}    

build .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'
}

1 个答案:

答案 0 :(得分:-1)

buildTypes {
    debug{
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

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