Fayaz Aralikatti Fayaz Aralikatti - 4 months ago 9
Java Question

Error while reading data from Realm in android

I was recently introduced to Realm Database for android. It seems very easy compared to SQLite, I made a simple app to test it. The data is getting saved, but I can't retrieve it for some reason.



Here's the 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();
}
}


An error pops us whenever I call the function to 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)

Answer

You should be passing a RealmConfiguration to Realm.getInstance() not getApplicationContext(). It should look something like this.

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