RealmExample已停止

时间:2017-10-27 11:48:07

标签: java android realm

我是领域数据库的新手。我开发了一个小应用程序。一切都很好,Gradle成功完成,没有任何错误。但是,当我点击按钮(添加,查看,删除和更新)时,应用程序关闭并说RealmExample(我的应用程序名称)已停止。

这是我的MainActivity.java代码:

package com.opent.realmexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;

import io.realm.Realm;
import io.realm.RealmResults;

import static com.opent.realmexample.R.id.btn_add;
import static com.opent.realmexample.R.id.btn_view;


public class MainActivity extends AppCompatActivity {

     Button add,view,delete,update;
    EditText roll_no,name;
    TextView text;
    Realm realm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = (Button) findViewById(R.id.btn_add);
        view = (Button) findViewById(btn_view);
        delete = (Button) findViewById(R.id.btn_delete);
        update = (Button) findViewById(R.id.btn_update);
        roll_no = (EditText) findViewById(R.id.roll_no);
        name = (EditText) findViewById(R.id.name);
        text = (TextView) findViewById(R.id.text);

        Realm.init(this);
        Realm realm = Realm.getDefaultInstance();
    }

     public void ClickAction(View view){
        switch (view.getId()){
             case R.id.btn_add : addRecords();
                 break;
             case R.id.btn_view : viewRows();
                 break;
             case R.id.btn_delete : deleteRecords();
                 break;
             case R.id.btn_update : updateRecords();

         }

     }


    private void addRecords() {
        realm.beginTransaction();
        Student student = realm.createObject(Student.class);
        student.setRoll_no(Integer.parseInt(roll_no.getText().toString()));
        student.setStudent_name(name.getText().toString());

        realm.commitTransaction();
    }

    private void viewRows() {
        RealmResults<Student> results = realm.where(Student.class).findAll();
        text.setText("");
        for(Student student : results){
            text.append(student.getRoll_no() + " " + student.getStudent_name() + "\n");
        }
    }

    private void deleteRecords() {

        RealmResults<Student> results = realm.where(Student.class).equalTo("roll_no",Integer.parseInt(roll_no.getText().toString())).findAll();
        realm.beginTransaction();
        results.deleteAllFromRealm();
        realm.commitTransaction();

    }

    private void updateRecords() {
        RealmResults<Student> results = realm.where(Student.class).equalTo("roll_no",Integer.parseInt(roll_no.getText().toString())).findAll();
        realm.beginTransaction();
        for (Student student: results) {
            student.setStudent_name(name.getText().toString());
        }
        realm.commitTransaction();
    }

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

这里是模型类Student.java 包com.opent.realmexample;

import io.realm.RealmObject;



public class Student extends RealmObject {

    public int roll_no;
    private String student_name;

     public int getRoll_no(){
         return roll_no;
     }
     public void setRoll_no(int roll_no){
         this.roll_no = roll_no;

     }
     public String getStudent_name(){
         return student_name;
     }
     public void  setStudent_name(String student_name){
         this.student_name= student_name;
     }
}

我添加了依赖类路径&#34; io.realm:realm-gradle-plugin:2.2.1&#34;在我的项目级别构建和  插件类路径&#34; io.realm:realm-gradle-plugin:2.2.1&#34;在我的应用程序级别构建太..但问题在哪里?请帮助我。

0 个答案:

没有答案