RealmConfiguration中的android Realm错误

时间:2018-05-03 02:01:28

标签: android realm

我是Realm数据库中的新手...这是appm for realm DB中的写字符串并读取它...

当我运行我的应用时,它会告诉我已停止

我在Logcat ...

中显示的RealmConfiguration目录中的错误

这是我的代码:

  

ObjectRealm类

public class ObjectRealm extends RealmObject {

@PrimaryKey
private int id;
private String myTitle ;

 public ObjectRealm() {
 }

 public ObjectRealm(int id, String myTitle) {
    this.id = id;
    this.myTitle = myTitle;
 }

 public int getId() {
    return id;
 }

 public void setId(int id) {
    this.id = id;
 }

 public String getMyTitle() {
    return myTitle;
 }

 public void setMyTitle(String myTitle) {
    this.myTitle = myTitle;
 }

 @Override
 public String toString() {
    return "ObjectRealm{" +
            "id=" + id +
            ", myTitle='" + myTitle + '\'' +
            '}';
 }

}
  

BaseApplication类

public class BaseApplication extends Application {


@Override
public void onCreate() {
    super.onCreate();

    String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();

    Realm.init(this);

    RealmConfiguration config = new RealmConfiguration.Builder()
            .name("myRealm")
            .directory(new File(rootPath + "/in MyFile/"))
            .build();

    Realm realm = Realm.getInstance(config);

}

}
  

MainActivity

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Realm realm = Realm.getDefaultInstance();
    realm.beginTransaction();

    for(int i =0 ;i<5;i++){
        ObjectRealm person = new ObjectRealm(i+1 ,"Title number : " + i+1);
    }
    realm.commitTransaction();
    showMe();

}

private void showMe() {
    Realm realm = Realm.getDefaultInstance();
    RealmQuery<ObjectRealm> query = realm.where(ObjectRealm.class);
    RealmResults<ObjectRealm> result = query.findAll();
    for (ObjectRealm e : result) {
        Log.d("asd", "showMe: " + e.toString());
    }
}
}
  

我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.tryrealm">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:name=".BaseApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    </manifest>

我添加了写入和读取外部存储的权限以及BaseApplication的名称

为什么它不起作用?我的错误在哪里?

当我更改

中的代码时
  

BaseApplication类

像这样

public class BaseApplication extends Application {


@Override
public void onCreate() {
    super.onCreate();

    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
            .build();
    Realm.setDefaultConfiguration(realmConfiguration);

}

}

我遇到了像这样的错误

error: Builder(Context) is not public in Builder; cannot be accessed from outside package
    RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();

1 个答案:

答案 0 :(得分:0)

@Override
public void onCreate() {
    super.onCreate();

    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
            .build();
    Realm.setDefaultConfiguration(realmConfiguration);

}

应该是

@Override
public void onCreate() {
    super.onCreate();

    Realm.init(this);
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
            .build();
    Realm.setDefaultConfiguration(realmConfiguration);

}

自Realm 2.0.0及更新版