使用游标读取数据库时出错
error when reading the database using cursor
当我尝试 return ArrayAdapter 的 readBase() 方法时。我在管理器 class.
中收到 NullPointerException 游标错误
主要Activity
public class Price Product Activity{
private Manager manager;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prise_product);
manager = new Manager(this);
spinner=(Spinner) findViewById(R.id.spinner);
spinnerAdapter();
}
@Override
protected void onResume() {
super.onResume();
manager.openBase();
}
public void spinnerAdapter(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
manager.readBase());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
经理
public class 经理 {
public Manager(Context context){
this.context = context;
helper=new Helper(context);
}
public void openBase(){
db = helper.getWritableDatabase();
}
public void insertBase(String name, Double gram){
ContentValues contentValues = new ContentValues();
contentValues.put(Constants.NAME, name);
contentValues.put(Constants.GRAM, gram);
db.insert(Constants.TABLE_NAME, null, contentValues);
}
public List<String> readBase(){
List<String> tempList = new ArrayList<>();
Cursor cursor = db.query(Constants.TABLE_NAME, null, null,
null, null, null, null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndexOrThrow
(Constants.NAME));
/*String gram = cursor.getString(cursor.getColumnIndexOrThrow
(Constants.GRAM));*/
tempList.add(name);
cursor.close();
}
return tempList;
}
}
错误代码
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.example.sweet.base.Manager.readBase(Manager.java:35)
对于很多人来说,答案是他们没有打开数据库。但是,我打开了数据库。
问题出在这一行 Cursor cursor = db.query(Constants.TABLE_NAME, null, null, null, null, null, null);你必须从数据库中读取数据并存储在游标中..但你正在通过游标
设置数据
public void Cursor dsiplayData() {
SQLiteDatabase sqLiteDatabase = openBase()
return sqLiteDatabase.rawQuery("SELECT * FROM +TABLE_NAME+", null)
}
public List<String> readBase(){
List<String> tempList = new ArrayList<>();
Cursor cursor = dsiplayData()
while (cursor.moveToNext()){
String name = cursor.getString(0); //if you want to read mode data from database you have to create a data model class
tempList.add(name);
cursor.close();
}
return tempList;
}
当我尝试 return ArrayAdapter 的 readBase() 方法时。我在管理器 class.
中收到 NullPointerException 游标错误主要Activity
public class Price Product Activity{
private Manager manager;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prise_product);
manager = new Manager(this);
spinner=(Spinner) findViewById(R.id.spinner);
spinnerAdapter();
}
@Override
protected void onResume() {
super.onResume();
manager.openBase();
}
public void spinnerAdapter(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
manager.readBase());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
经理 public class 经理 {
public Manager(Context context){
this.context = context;
helper=new Helper(context);
}
public void openBase(){
db = helper.getWritableDatabase();
}
public void insertBase(String name, Double gram){
ContentValues contentValues = new ContentValues();
contentValues.put(Constants.NAME, name);
contentValues.put(Constants.GRAM, gram);
db.insert(Constants.TABLE_NAME, null, contentValues);
}
public List<String> readBase(){
List<String> tempList = new ArrayList<>();
Cursor cursor = db.query(Constants.TABLE_NAME, null, null,
null, null, null, null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndexOrThrow
(Constants.NAME));
/*String gram = cursor.getString(cursor.getColumnIndexOrThrow
(Constants.GRAM));*/
tempList.add(name);
cursor.close();
}
return tempList;
}
}
错误代码
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.example.sweet.base.Manager.readBase(Manager.java:35)
对于很多人来说,答案是他们没有打开数据库。但是,我打开了数据库。
问题出在这一行 Cursor cursor = db.query(Constants.TABLE_NAME, null, null, null, null, null, null);你必须从数据库中读取数据并存储在游标中..但你正在通过游标
设置数据 public void Cursor dsiplayData() {
SQLiteDatabase sqLiteDatabase = openBase()
return sqLiteDatabase.rawQuery("SELECT * FROM +TABLE_NAME+", null)
}
public List<String> readBase(){
List<String> tempList = new ArrayList<>();
Cursor cursor = dsiplayData()
while (cursor.moveToNext()){
String name = cursor.getString(0); //if you want to read mode data from database you have to create a data model class
tempList.add(name);
cursor.close();
}
return tempList;
}