如何使用android手机运行realm而不崩溃?

h7appiyu  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(316)

所以我刚开始使用领域5.8,我不能用移动设备运行它只是崩溃的应用程序没有任何错误显示?我该怎么办?
调用init和defaultconf

import android.app.Application;
 import io.realm.Realm;
 import io.realm.RealmConfiguration;

 public class RealmApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
        Realm.setDefaultConfiguration(realmConfiguration);
    }
   }

现在活动文件在那里,我们正试图发送输入字段username和user\ id到数据库。我已经销毁了getdefaultinstance(),但似乎什么都没用,只是崩溃了?我是否需要下载数据库到手机,以使这项工作?好吧,我不知道比我看起来更不明白。

Realm realm;
 @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.friends);
 username = (EditText)findViewById(R.id.editText);
    user_id = (EditText)findViewById(R.id.editText);
    submit = (Button)findViewById(R.id.button2);
    submit.setOnClickListener(this);

    realm = Realm.getDefaultInstance();
    }

     public void onClick(View view) {
    writeToDB(username.getText().toString().trim(), Integer.parseInt(user_id.getText().toString().trim()));
  }

 public void writeToDB(String username, int user_id) {
realm.executeTransactionAsync(new Realm.Transaction() {
    @Override
    public void execute(Realm bgRealm) {
        Friend user = bgRealm.createObject(Friend.class);
        user.setUsername(username);
        user.setUser_id(user_id);
    }
}, new Realm.Transaction.OnSuccess() {
    @Override
    public void onSuccess() {
        // Transaction was a success.
        Log.v("Database","database success");
    }
}, new Realm.Transaction.OnError() {
    @Override
    public void onError(Throwable error) {
        // Transaction failed and was automatically canceled.
    }
});
}

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

iecba09b1#

好吧,你不应该使用xampp和你自己的设置重置xampp,现在我可以运行mysqli和领域在同一个项目!

相关问题