Reputation: 106
private void addAction() {
String what = mInputWhat.getText().toString();
long now = System.currentTimeMillis();
Realm.init(getContext());
RealmConfiguration configuration = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(configuration);
Realm realm = Realm.getDefaultInstance();
Drop drop = new Drop(what, now, 0, false);
realm.beginTransaction();
realm.copyToRealm(drop);
realm.commitTransaction();
realm.close();
}
}
Here whenever i don't create a contexet the app crashes but when i create a context using Realm.init(getContext()) the "Add it" button don't workenter image description here
Upvotes: 0
Views: 390
Reputation: 835
You must init Realm in a class that extended from Application.see code below :
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.name("appName.realm")
.schemaVersion(1)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
}
}
after it u can call Realm class anywhere
don't forget add this line to your manifest application tag
android:name="com.example.MyApplication"
Upvotes: 1