无法从ROOM数据库检索数据

时间:2019-10-08 17:27:39

标签: android android-studio android-room

我正在使用android studio开发一个android应用程序,我有从ROOM数据库中检索数据的方法,但是存在运行时错误,我无法解决。问题不是因为上下文

这是从数据库检索的代码(活动)

public class cartActivity extends AppCompatActivity  {
RecyclerView recycler_cart;
Button btn_place_order;
CompositeDisposable compositionDisposable;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_cart );

    compositionDisposable = new CompositeDisposable();
    recycler_cart = (RecyclerView) findViewById( R.id.recycler_cart );
    recycler_cart.setLayoutManager( new LinearLayoutManager( this ) );
    recycler_cart.setHasFixedSize( true );

    btn_place_order = (Button) findViewById( R.id.btn_place_order );
    loadCartItems(); //it was a comment!!!!!
}


@Override
protected void onStop() {
    compositionDisposable.clear();
    super.onStop();
}

@Override
protected void onDestroy() {
    compositionDisposable.clear();
    super.onDestroy();
}

private void loadCartItems() {

    compositionDisposable.add(

          Common.cartRepository.getCartItems().observeOn(AndroidSchedulers.mainThread())
                  .subscribeOn(Schedulers.io() )
                  .subscribe( new Consumer<List<Cart>>() {
                      @Override
                      public void accept(List<Cart> carts) {
                          displayCartItem(carts);
                      }
                  } )



    );


}
private void displayCartItem(List<Cart> carts){
    CartAdapter cartAdapter=new CartAdapter(this,carts);
    recycler_cart.setAdapter(cartAdapter);
}
}

这是将项目保存在数据库中的类(片段)

 addBTn.setOnClickListener( new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Cart cart=new Cart(productNameTxt.getText().toString());
                                cart.setName( (String) productNameTxt.getText() );
                                //cart.setPrice(Integer.parseInt( (String ) productPriceTxt.getText()  ));
                                CartDatabase.getInstance(getActivity().getApplicationContext()).cartDAO().insertToCart(cart); //?

                                Toast.makeText(getActivity(),"Item added successfully",Toast.LENGTH_SHORT).show();

这是错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.queueskip, PID: 3544
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at com.example.queueskip.Database.Local.CartDatabase.getInstance(CartDatabase.java:25)
    at com.example.queueskip.ui.home.HomeFragment$2$1$1.onClick(HomeFragment.java:169)
    at android.view.View.performClick(View.java:5721)
    at android.widget.TextView.performClick(TextView.java:10936)
    at android.view.View$PerformClick.run(View.java:22620)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7406)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

0 个答案:

没有答案