gpt4 book ai didi

java - 当我打开模块时它崩溃

转载 作者:行者123 更新时间:2023-12-01 18:08:45 27 4
gpt4 key购买 nike

我正在创建 android 项目,当我打开 foodlist 模块时,它应该显示带有描述的数据图像,但它崩溃了,logcat 显示

由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.database.Cursor com.example.pointofsale.DatabaseHelper.getItemsdata(java.lang.String)”
在 com.example.pointofsale.FoodList.onCreate(FoodList.java:31)

如何解决这个问题?

public class FoodList extends AppCompatActivity {

GridView gridView;
ArrayList<Food> list;
FoodListAdapter adapter = null;

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

gridView = (GridView) findViewById(R.id.foodgridviewID);
list = new ArrayList<>();
adapter = new FoodListAdapter(this, R.layout.food_items,list);
gridView.setAdapter(adapter);

//get data from database

Cursor cursor = Add_Items.databaseHelper.getItemsdata("SELECT * FROM ITEMS");
list.clear();
while (cursor.moveToNext())
{
int id = cursor.getInt(0);
String name = cursor.getString(1);
String price = cursor.getString(2);
byte[] image = cursor.getBlob(3);

list.add(new Food(name, price, image, id));
}

adapter.notifyDataSetChanged();
if (list.size() == 0)
{
Toast.makeText(this,"No record found", Toast.LENGTH_SHORT).show();
}
}
}

//数据库

public Cursor getItemsdata (String sql)
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery(sql, null);
}

最佳答案

在您的代码中,Add_Items.databaseHelpernull

确保您初始化了 Add_Items.databaseHelper 的实例。

关于java - 当我打开模块时它崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60506698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com