Recyclerview Force Closing内的Android Realm数据库

时间:2017-05-23 10:22:15

标签: android realm

这是我在Recyclerview的onBindViewHolder中的代码,它删除了所选的 列表中的项目和Realm DB。

  @Override
  public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(c).inflate(R.layout.listing,parent,false);
    return new MyHolder(v);
  }

 holder.cardview.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            RealmResults<Sample> results = realm.where(Sample.class).findAll();

            realm.beginTransaction();
            results.remove(position);
            realm.commitTransaction();


            return false;
        }
    });

这是Holder的代码。

 public class MyHolder extends RecyclerView.ViewHolder {

    TextView in_name,in_address,in_age;
    ImageView profile_img;
    CardView cardview;

    public MyHolder(View itemView) {
        super(itemView);

        in_name= (TextView) itemView.findViewById(R.id.in_name);
        in_address= (TextView) itemView.findViewById(R.id.in_address);
        in_age= (TextView) itemView.findViewById(R.id.in_age);
        profile_img= (ImageView) itemView.findViewById(R.id.profile_img);
        cardview = (CardView) itemView.findViewById(R.id.cardview);
    }
}

代码的结果是删除了所选项目,但app force关闭了 跟随错误。

FATAL EXCEPTION: main
Process: cozycodes.project.realmsample, PID: 20629
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.ViewParent.showContextMenuForChild(android.view.View)' on a null object reference
at android.view.View.showContextMenu(View.java:4853)
at android.view.View.performLongClick(View.java:4822)
at android.view.View$CheckForLongPress.run(View.java:19840)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我已经附上了我的MainActivity的onCreate和OnDestroy。

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


    sample_recyclerview= (RecyclerView) findViewById(R.id.sample_recycler);
    sample_recyclerview.setLayoutManager(new LinearLayoutManager(this));

    this.realm = RealmConnect.with(this).getRealm();



    final RealmConnect helper=new RealmConnect(realm);
    helper.retrieveFromDB();


    adapter=new SampleAdapter(this,helper.justRefresh());
    sample_recyclerview.setAdapter(adapter);


    realmChangeListener=new RealmChangeListener() {
        @Override
        public void onChange() {

            adapter=new 
SampleAdapter(MainActivity.this,helper.justRefresh());
            sample_recyclerview.setAdapter(adapter);
        }
    };


    realm.addChangeListener(realmChangeListener);

    FloatingActionButton fab = (FloatingActionButton) 
  findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            displayInputDialog();
        }
    });
}


 @Override
 protected void onDestroy() {
    super.onDestroy();

    realm.removeChangeListener(realmChangeListener);
    realm.close();
 }

3 个答案:

答案 0 :(得分:1)

 RealmResults<Sample> results = realm.where(Sample.class).findAll();
 Sample b = results.get(position);
 String title = b.getName();
 realm.beginTransaction();
 results.remove(position);
 realm.commitTransaction();  

我将这段代码放在一个警告对话框中,它对我有用。

答案 1 :(得分:0)

stacktrace表明存在Nullpointer引用:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.ViewParent.showContextMenuForChild(android.view.View)' on a null object reference

答案 2 :(得分:-1)

我没有确切的想法你的问题是什么,但我有一个从recyclerView删除领域数据的问题。使用realm.copyFromRealm从领域获取数据时复制数据。

Object get(@Nullable Integer i);

例如

realm.copyFromRealm(YOUR REALM QUERY)