gpt4 book ai didi

java - 'getInstance(io.realm.RealmConfiguration )' in ' io.realm.Realm' cannot be applied to '(android.content.Context)' 错误

转载 作者:行者123 更新时间:2023-11-29 23:16:16 25 4
gpt4 key购买 nike

我正在使用 Realm 添加一个新数据库,当我添加时 Realm realm = Realm.getInstance(getApplicationContext());我得到这个错误

 'getInstance(io.realm.RealmConfiguration)' in 'io.realm.Realm' cannot be applied to '(android.content.Context)'

我爱不释手

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mobile);
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.build();
Realm.setDefaultConfiguration(realmConfiguration);

final EditText mobType =(EditText) findViewById(R.id.editText1);
final EditText mobModel = (EditText)findViewById(R.id.editText2);
final EditText mobStorage =(EditText) findViewById(R.id.editText3);
final EditText mobRam =(EditText) findViewById(R.id.editText4);

Button addMob = findViewById(R.id.addbutton2);
addMob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MobileJ mobilej = new MobileJ();
mobilej.setMobtype(mobType.getText().toString());
mobilej.setMobmodel(mobModel.getText().toString());
mobilej.setMobRAM(mobRam.getText().length());
mobilej.setMobstorge(mobStorage.getText().length());

Realm realmm = Realm.getDefaultInstance(getApplicationContext());
realmm.beginTransaction();
realmm.copyToRealmOrUpdate(mobilej);
realmm.commitTransaction();

最佳答案

您可以通过两种方式获取 Realm 实例:

  1. Realm.getDefaultInstance();

您不需要向该方法传递任何参数。在您的代码中,您这样做了。

  1. Realm.getInstance(config);

此方法需要 RealmConfig 而不是 Context。

关于java - 'getInstance(io.realm.RealmConfiguration )' in ' io.realm.Realm' cannot be applied to '(android.content.Context)' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55311857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com