[英]RXJAVA ROOM android
Trying to learn Room and RXJAVA.尝试学习 Room 和 RXJAVA。 I have about 80% of this understood but I'm getting stuck on figuring the rest out.
我已经了解了大约 80%,但我一直在想办法解决其余的问题。
Here is the error I get on the insert data.这是我在插入数据上遇到的错误。
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.learnroom.EntityDao.insert(com.example.learnroom.Entitys)' on a null object reference
java.lang.NullPointerException:尝试在空对象引用上调用接口方法“void com.example.learnroom.EntityDao.insert(com.example.learnroom.Entitys)”
If I don't run the try catch I get the following error which seems to be related.如果我不运行 try catch,我会收到以下似乎相关的错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.learnroom/com.example.learnroom.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'io.reactivex.Maybe com.example.learnroom.EntityDao.getEntity(java.lang.String)' on a null object reference
java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.learnroom/com.example.learnroom.MainActivity}:java.lang.NullPointerException:尝试调用接口方法 'io.reactivex.Maybe com.example.learnroom。 EntityDao.getEntity(java.lang.String)' 在一个空对象引用上
How do I fix this?我该如何解决?
I have tried to simplify from the tutorials all over the web most using recyclerviews to just 2 text fields.我试图将网络上大多数使用 recyclerviews 的教程简化为仅 2 个文本字段。 They say this is 3 pieces but it doesn't seem like it, as the DB was never set up so I ran it in a method to run the code.
他们说这是 3 个部分,但看起来不像,因为数据库从未设置过,所以我用一种方法运行它来运行代码。 Maybe someone can help explain to me how this really works.
也许有人可以帮我解释一下这是如何真正起作用的。 my code
我的代码
Dao道
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();
}
Entity实体
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数据库
@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;
}
}
MainActivity主要活动
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);
}
}
Reason for crash is this line in your PathwaysDB class崩溃的原因是你的PathwaysDB类中的这一行
public static EntityDao entityDao() {
return null;
}
it is returning null.它返回空值。 It should be like
它应该像
public abstract EntityDao entityDao()
You forget to add @Dao annonation to your EntityDao interface class.您忘记将 @Dao 注释添加到您的 EntityDao 接口类中。 also you need to change below method :
您还需要更改以下方法:
public static EntityDao entityDao() {
return null;
}
To到
public abstract EntityDao entityDao();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.