我使用shared pref来存储我的灯的数组列表,因为我使用这些数据将其放入自定义数组适配器的列表视图中,但每次它在我将数据转换为json以保存在shared pref中的行处崩溃
字符串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();
}
}
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);
}
}
建造。梯度
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'
}
buildTypes {
debug{
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
将这几行添加到build.gradle文件并学习proguard
我有一个这样的旋转器: 当用户点击时,我希望它保存状态: 它将位置保存在SharedPref中,但微调器将返回默认值。有人看到什么了吗?
我正在做一个项目,我想要一个功能,在WhatsApp上给我们留言,为此,我使用了使用intent的WhatsApp API。在“WhatsApp上向我们发送消息”功能中,我使用了2-3个不同的数字,并将其存储在列表中,并使用random()方法从列表中检索该数字,正如我所希望的那样,每当用户使用该功能时,他/她每次都必须连接不同的数字。但现在我想更改列表中存储的数字。 那么,我如何在每次不改变实际
问题内容: 我正在尝试以android应用程序的形式创建一个小型课程聚合程序。 我的课程全部存储在Firebase实时数据库中,该数据库可从firebase控制台查看,并且一切正常。 问题是我已经编写了一个Java方法来连接到DB,从DB检索数据,将数据转换为Custom Java对象,将其附加到另一个Custom Java对象,然后将该对象保存到ArrayList中。 与数据库的连接已成功建立,
我正在尝试保存数据透视表中多列的数据。但是,数据没有保存。下面是我正在做的事情: 表 刀片 如果我只使用checkbox来同步benefit_id和user_id,它就能工作。但如果添加额外的数据amount1和amount2,则不起作用。 控制器 事先谢谢你的帮助。
我在共享首选项中保存列表并将其显示在列表视图中没有问题,但我的问题是当我重新启动应用程序并尝试将项目添加到列表视图时 旧的存储项目已删除.有我的代码: } public void getDates(){
我试图在kotlin中的两个活动之间传递一个值,但如果我使用下面的代码,那么我只得到“Hello World”默认值,而不是PREFERENCE_NAME值。我的文本ID名称是android: id="@id/tv_count"感谢任何帮助。