当前位置: 首页 > 知识库问答 >
问题:

RXJAVA ROOM android

郎灿
2023-03-14

试图学习房间和RXJAVA。我已经理解了大约80%的内容,但我仍然无法弄清楚其余的内容。

这是我在插入数据时得到的错误。

Java语言lang.NullPointerException:尝试调用接口方法“void com”。实例学习室。实体道。在空对象引用上插入(com.example.learnroom.Entitys)“”

如果我不运行try catch,我会得到以下错误,这似乎是相关的。

Java语言lang.RuntimeException:无法启动activity ComponentInfo{com.example.learnroom/com.example.learnroom.MainActivity}:java。lang.NullPointerException:尝试调用接口方法“io”。反应性X。也许是com。实例学习室。实体道。空对象引用上的getEntity(java.lang.String)“”

我该怎么解决这个问题?

我试图将网络上大多数使用回收器视图的教程简化为只有2个文本字段。他们说这是3个部分,但看起来不像,因为DB从未设置过,所以我用一种方法运行它来运行代码。也许有人可以帮助向我解释这是如何工作的。我的代码

public interface EntityDao {

 @Query("SELECT * FROM Entitys WHERE ID  = :ID LIMIT 1")
Maybe<List<Entitys>> getEntity(String ID);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Entitys entitys);

@Query("DELETE FROM Entitys")
void deleteAllEntity();
}

实体

 public class Entitys {

@PrimaryKey
@NonNull
public String ID;

public  String ts;
public  String tss;

public Entitys(@NonNull String ID, String ts, String tss) {
    this.ID = ID;
    this.ts = ts;
    this.tss = tss;
}

public String getTss() {
    return tss;
}

public void setTss(String tss) {
    this.tss = tss;
}

public void setID(String ID) {
    this.ID = ID;
}

public void setTs(String ts) {
    this.ts = ts;
}

public String getID() {
    return ID;
}

public String getTs() {
    return ts;
}
 } 

数据库

 @Database(entities = {Entitys.class}, version = 1)
 public abstract class PathwaysDB extends RoomDatabase {

private static volatile PathwaysDB INSTANCE;

public static EntityDao entityDao() {
    return null;
}

public static PathwaysDB getInstance(Context context) {
    if (INSTANCE == null) {
        synchronized (PathwaysDB.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        PathwaysDB.class, "Pathwaysdb")
                        .build();
            }
        }
    }
    return INSTANCE;
}


 }

主要活动

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = MainActivity.class.getSimpleName();
Button tb;
EditText te, tes;
String ts, tss, ID;

CompositeDisposable compositeDisposable = new CompositeDisposable();

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

    ID ="test";
    te = findViewById(R.id.te);

    tb = findViewById(R.id.tb);
    tb.setOnClickListener(this);

    tes = findViewById(R.id.tes);

    Builddb();

    try{
    getData();}catch (Exception e){}
}

   private void Builddb() {

    Completable.fromAction(() -> PathwaysDB.getInstance(this))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new CompletableObserver() {
                @Override
                public void onSubscribe(Disposable d) {
                    compositeDisposable.add(d);
                }

                @Override
                public void onComplete() {
                    // action was completed successfully
                }

                @Override
                public void onError(Throwable e) {
                    // something went wrong
                }
            });
}

private void getData() {

    Maybe<List<Entitys>> single = entityDao().getEntity(ID);
    single.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new MaybeObserver<List<Entitys>>() {
                @Override
                public void onSubscribe(Disposable d) {
                    // add it to a CompositeDisposable
                }

                @Override
                public void onSuccess(List<Entitys> entity) {
                    te.setText(entity.indexOf(ts));
                    tes.setText(entity.indexOf(tss));
                }
                @Override
                public void onError(Throwable e) {
                    // show an error message
                }

                @Override
                public void onComplete() {
                }
            });

    compositeDisposable.add((Disposable) single);
}

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

    compositeDisposable.dispose();
}

private void updateUserName() {

   ts = te.getText().toString();
   tss = tes.getText().toString();
   Entitys entitys = new Entitys(ID, ts, tss);

    Completable.fromAction(() -> entityDao().insert(entitys))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new CompletableObserver() {
                @Override
                public void onSubscribe(Disposable d) {
                    compositeDisposable.add(d);
                }

                @Override
                public void onComplete() {
                    // action was completed successfully
                }

                @Override
                public void onError(Throwable e) {
                    // something went wrong
                }
            });


}

@Override
public void onClick(View view) {
    updateUserName();

    Intent forward = new Intent(this, secondpage.class);
    startActivity(forward);
}
 }

共有2个答案

宁兴修
2023-03-14

你忘记添加@道anonation到你的实体道接口类。你还需要改变下面的方法:

public static EntityDao entityDao() {
    return null;
}

public abstract EntityDao entityDao();
戴高远
2023-03-14

崩溃的原因是您的PathwaysDB类中的这一行

public static EntityDao entityDao() {
return null;
}

它正在返回null。应该是这样的

public abstract EntityDao entityDao()
 类似资料:

相关问答

相关文章

相关阅读