@EBean
public class CartDB {
@OrmLiteDao(helper = DatabaseHelper.class, model = Cart.class)
CartDao cartDao;
public Cart getCart() {
return cartDao.getCart()}
public String count(){
long count =0;
try {
count= cartDao.countOf();
} catch (SQLException e) {
e.printStackTrace();
}
return count+"";
}
在我的第二个Activity onCreate()中,我调用了Count方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Log.e("Count",cartDB.count()+"");
}
比count方法返回数据, 但在我的第一个活动onCreate()中,我调用了count方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout);
Log.e("Count",cartDB.count()+"");
}
nullpointer exeption和我的应用崩溃
注意:从Sqlite数据库中检索数据
答案 0 :(得分:3)
空指针异常是因为您尚未在Activity中初始化Java Class CardDB;
请制作CartDB的实例:
CartDB mycart;
然后在你的Activity的onCreate方法中正确初始化它:
onCreate(Bundle savedInstance){
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout);
mycart=new CartDB();
}