我尝试将对象插入Realm数据库,插入对象,但它的字段变为“null”。
这是所有代码。在“person.setAge(14);”行我可以看到,当放一个断点时,那个person.Name已经为null,但我刚刚设置了一个值!
活动代码:
package wovilonapps.realmtraining;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import io.realm.Realm;
public class MainActivity extends AppCompatActivity {
Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
realm = Realm.getInstance(this);
realm.beginTransaction();
Person person = realm.createObject(Person.class);
person.setName("Young Person");
person.setAge(14);
realm.commitTransaction();
ArrayList<Person> result = new ArrayList(realm.where(Person.class).findAll());
Log.d("MyLOG", "ds");
}
}
班级人员代码:
package wovilonapps.realmtraining;
import io.realm.RealmObject;
public class Person extends RealmObject {
private String Name;
private int Age;
public String getName() {
return Name;
}
public int getAge() {
return Age;
}
public void setName(String name) {
Name = name;
}
public void setAge(int age) {
Age = age;
}
}