在Android中从Realm读取数据时出错

时间:2016-07-29 19:41:47

标签: java android realm android-database

我最近被介绍到Android的Realm数据库。与SQLite相比,它似乎很容易,我做了一个简单的应用程序来测试它。数据已保存,但由于某种原因我无法检索。


这是MainActivity

package com.fayaz.realm;

public class MainActivity extends AppCompatActivity {


public Realm realm;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    btn=(Button)findViewById(R.id.btn);

    realm=Realm.getInstance(getApplicationContext());

    //WRITE
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final EditText taskEditText = new EditText(MainActivity.this);
            AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Add a new task")
                    .setMessage("What do you want to do next?")
                    .setView(taskEditText)
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            String task = String.valueOf(taskEditText.getText());
                            realm.beginTransaction();
                            Person person=realm.createObject(Person.class);
                            person.setName(task);
                            Toast.makeText(getApplicationContext(),"ADDED",Toast.LENGTH_SHORT).show();
                            realm.commitTransaction();
                        }
                    })
                    .setNegativeButton("Cancel", null)
                    .create();
            dialog.show();
        }
    });

    //READ
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Execute the query:
            RealmResults<Person> results = realm.where(Person.class).findAll();
            String name=results.toString();
            Toast.makeText(getApplicationContext(),name,Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
protected void onDestroy(){
    super.onDestroy();
    realm.close();
    }
}

每当我将函数调用为READ Data


时,就会弹出错误 java.lang.UnsatisfiedLinkError: No implementation found for long io.realm.internal.TableView.nativeSync(long) (tried Java_io_realm_internal_TableView_nativeSync and Java_io_realm_internal_TableView_nativeSync__J)

2 个答案:

答案 0 :(得分:0)

您应该将RealmConfiguration传递给Realm.getInstance()而不是getApplicationContext()。看起来应该是这样的。

RealmConfiguration config = new RealmConfiguration.Builder(context)
  .name("myrealm.realm")
  .build();
Realm realm = Realm.getInstance(config);

答案 1 :(得分:0)

您应该至少更新到Realm 1.0.0,但最新版本(在撰写本文时为Realm 1.1.1)。

您正在使用Realm.getInstance(Context)0.91.0最近已将其删除。

要使用Realm的最新版本,您需要使用Android Studio和Gradle,在项目级build.gradle中添加它

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:1.1.1" //this line
    }
}

此外,您还需要在模块级build.gradle中应用realm-android Gradle插件:

apply plugin: 'com.android.application'
apply plugin: 'realm-android' // this line

谈论过时版本中的错误并没有多大意义。它可能像一年前一样被修复了。

编辑:

//READ
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Execute the query:
        RealmResults<Person> results = realm.where(Person.class).findAll();
        if(!results.isEmpty()) {
            Person person = results.get(0);
            String name = person.getName();
            Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
        }            
    }
});