本文介绍了打开数据库sqlite android时出错null异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好,我在尝试在sqlite android中打开数据库时遇到了问题。我想将查询中的数据与来自textview的数据进行匹配,但是当应用程序运行时,强制关闭。我看到了log cat和用于open db的方法是null异常。 这是db类的完整代码 public class DBReminder { 私人 SQLiteDatabase db; private DBHelper helper; private 上下文konteks; private static final String DBName = db_reminder.db; private static final String TableName = task_table; public static final String ID = id; public static final String LATITUDE = 纬度; public static final String LONGITUDE = 经度; public static final String RADIUS = radius; public static final String ALAMAT = alamat; public static final String KONTEKS_TUGAS = konteks_tugas; private static final String makeTable = create table + TableName + ( + ID + 整数主键自动增量, + LATITUDE + integer not null, + LONGITUDE + integer not null, + RADIUS + integer not null, + ALAMAT + varchar(50)not null, + KONTEKS_TUGAS + varchar(50)not null + );; private static class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context){ super (context,DBName,null, 1 ); // TODO自动生成的构造函数存根 } @覆盖 public void onCreate(SQLiteDatabase db){ // TODO自动生成的方法存根 Log.d( db, create table); db.execSQL(makeTable); } @覆盖 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ // TODO自动生成的方法存根 Log.d ( db, 升级表); db.execSQL( drop table if exists + TableName); } } public DBReminder(上下文c){ 此 .konteks = c; helper = new DBHelper(c); } public DBReminder openWrite(Context c) throws SQLException { helper = new DBHelper(c); db = helper.getWritableDatabase(); 返回 此; } public DBReminder openRead(Context c) throws SQLException { helper = new DBHelper(c); db = helper.getReadableDatabase(); 返回 此; } public void closeConn(){ helper.close(); } public void insertTask(Task tugas){ openWrite(konteks); ContentValues val = new ContentValues(); val.put(LATITUDE,tugas.getLatitude()); val.put(LONGITUDE,tugas.getLongitude()); val.put(RADIUS,tugas.getRadius()); val.put(ALAMAT,tugas.getAlamat()); val.put(KONTEKS_TUGAS,tugas.getKonteks()); db.insert(TableName,null,val); closeConn(); } } 我在这个功能中使用它 public void cekPosisi( String txtAlamat){ if (txtAlamat.toString()。length()> 0 ){ dbHandler.openRead(getApplicationContext()); 字符串 sql = select * from task_table其中alamat =' + txtAlamat + ';; 光标rs = db.rawQuery(sql,null); if (rs!= null){ rs.moveToFirst(); 字符串 addr = rs.getString(rs.getColumnIndex( alamat)); Toast.makeText( this , Anda ada tugas di + addr,Toast.LENGTH_LONG)。show(); } 其他 { Toast.makeText(此, Anda tidak ada tugas di lokasi sekarang,Toast.LENGTH_LONG)。show(); } dbHandler.closeConn(); rs.close(); } 其他 { Toast.makeText(此, Tidak ada alamat,Toast.LENGTH_LONG)。show(); } } 函数cekPosisi用于 // cek tugas txtAddr = teksAlamat.getText()的toString(); this .cariTugas.setOnClickListener( new OnClickListener(){ @覆盖 public void onClick(查看v){ // TODO自动生成的方法存根 Log.d( alamat textView,txtAddr); cekPosisi(txtAddr); } }); 请帮助我,谢谢...... 解决方案 在 public void cekPosisi(String txtAlamat) 而不是 dbHandler.closeConn(); rs.close(); 试试 rs.close(); dbHandler.closeConn(); 如果这不是我的话你可以粘贴logcat错误行号 Hi everyone, i have a problem while try to open database in sqlite android. I want to match the data in query with the data from textview but when the app run it force close. I saw the log cat and the method that use for open db is null exception.This is the complete code of db classpublic class DBReminder {private SQLiteDatabase db;private DBHelper helper;private Context konteks;private static final String DBName = "db_reminder.db";private static final String TableName = "task_table";public static final String ID = "id";public static final String LATITUDE = "latitude";public static final String LONGITUDE = "longitude";public static final String RADIUS = "radius";public static final String ALAMAT = "alamat";public static final String KONTEKS_TUGAS = "konteks_tugas";private static final String makeTable = "create table " + TableName + "(" + ID + " integer primary key autoincrement," + LATITUDE + " integer not null," + LONGITUDE + " integer not null," + RADIUS + " integer not null," + ALAMAT + " varchar(50) not null," + KONTEKS_TUGAS + " varchar(50) not null" + ");";private static class DBHelper extends SQLiteOpenHelper {public DBHelper(Context context) {super(context, DBName, null, 1);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubLog.d("db", "create table");db.execSQL(makeTable);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubLog.d("db", "upgrade table");db.execSQL("drop table if exists " + TableName);}}public DBReminder(Context c) {this.konteks = c;helper = new DBHelper(c);}public DBReminder openWrite(Context c) throws SQLException {helper = new DBHelper(c);db = helper.getWritableDatabase();return this;}public DBReminder openRead(Context c) throws SQLException {helper = new DBHelper(c);db = helper.getReadableDatabase();return this;}public void closeConn() {helper.close();}public void insertTask(Task tugas) {openWrite(konteks);ContentValues val = new ContentValues();val.put(LATITUDE, tugas.getLatitude());val.put(LONGITUDE, tugas.getLongitude());val.put(RADIUS, tugas.getRadius());val.put(ALAMAT, tugas.getAlamat());val.put(KONTEKS_TUGAS, tugas.getKonteks());db.insert(TableName, null, val);closeConn();}}And i use that in this functionpublic void cekPosisi(String txtAlamat) {if (txtAlamat.toString().length() > 0) {dbHandler.openRead(getApplicationContext());String sql = "select * from task_table where alamat = '"+txtAlamat+"';";Cursor rs = db.rawQuery(sql, null);if(rs != null) {rs.moveToFirst();String addr = rs.getString(rs.getColumnIndex("alamat"));Toast.makeText(this, "Anda ada tugas di " + addr, Toast.LENGTH_LONG).show();} else {Toast.makeText(this, "Anda tidak ada tugas di lokasi sekarang", Toast.LENGTH_LONG).show();}dbHandler.closeConn();rs.close();} else {Toast.makeText(this, "Tidak ada alamat", Toast.LENGTH_LONG).show();}}The function cekPosisi used in//cek tugastxtAddr = teksAlamat.getText().toString();this.cariTugas.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubLog.d("alamat textView", txtAddr);cekPosisi(txtAddr);}});Please help me, thanks... 解决方案 Inpublic void cekPosisi(String txtAlamat)instead ofdbHandler.closeConn();rs.close();tryrs.close();dbHandler.closeConn();and if this doesn''t work can you paste logcat error line number 这篇关于打开数据库sqlite android时出错null异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 19:03