gpt4 book ai didi

android - 尝试调用 getWritableDatabase() 时继续获取 NullPointerExceptions

转载 作者:IT王子 更新时间:2023-10-29 06:25:28 24 4
gpt4 key购买 nike

我是 Android 框架的新手,我无法通过 SQLite 的第一垒。

我正在尝试构建一个非常简单的应用程序,它有一个 EditText 搜索框,当按下某个键时,它会在 SQLite 数据库上执行 Like %word% 搜索,以查找在 EditText 框中输入的文本,并显示结果是一个 ListView。

注意下面的代码包括调试和注释代码,抱歉我还没抽空清理它。

激活是

public class sustainable_fish extends Activity {

private String keycodeToAscii(int arg2){

String retvar = "";

switch(arg2){
case KeyEvent.KEYCODE_A: retvar = "a";break;
case KeyEvent.KEYCODE_B: retvar = "b";break;

剪断了,但你明白了。

    case KeyEvent.KEYCODE_RIGHT_BRACKET: retvar = ")";break;
default: retvar = ""; break;
};
return retvar;

}



Context that = getApplicationContext();
fishDB dh = new fishDB(getApplicationContext());


private OnKeyListener search = new OnKeyListener() {

public boolean onKey(View arg0, int arg1, KeyEvent arg2) {


@SuppressWarnings("unused")
Cursor cur = null;
String searchData;
EditText myEditText = (EditText) findViewById(R.id.fish_search);
CharSequence edit_text_value = myEditText.getText();
searchData = edit_text_value.toString();
searchData = searchData + keycodeToAscii(arg2.getKeyCode() );
Log.d("onKey", "searchData = "+searchData);
/*
Commented out because as the database doesn't work, so theirs no point just yet searching.

cur = fish.search(searchData);
if (cur != null)
{
String[] displayFields = new String[] {cur.getString(2),
cur.getString(1)};

int[] displayViews = new int[] { R.id.fish_rank,
R.id.fish_name};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(that,
R.layout.text_list, cur,
displayFields, displayViews);

ListView myList=(ListView)findViewById(android.R.id.list);

myList.setAdapter(adapter);
}
*/

return false;
}

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
EditText searchBar = (EditText) findViewById(R.id.fish_search);
searchBar.setOnKeyListener(search);

}

}

SQLite 数据库由 fishDB 处理

  public class fishDB {

public static final String DATABASE_NAME = "fish.db3";
public static final int DATABASE_VERSION = 1;
private SQLiteDatabase database;
private Context myContext;
private static boolean booFirstrun = false;

fishDB(Context inContext){
myContext = inContext;
OpenHelper helper = new OpenHelper(myContext);
Log.w("fishDB", "Called OpenHelper");

// Here be the problem...

database = helper.getWritableDatabase();

}

public boolean firstRun(){
return booFirstrun;
}


private static class OpenHelper extends SQLiteOpenHelper {

private static final String CREATE_TABLE_FEEDS = "create table feeds (feed_id integer primary key autoincrement, "
+ "title text not null, url text not null);";

OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.w("OpenHelper", "Called superconstructor");
}

@Override
public void onCreate(SQLiteDatabase db) {
try {
Log.w("OpenHelper", "in onCreate");
booFirstrun = true;
Log.w("OpenHelper", "set booFirstrun");
db.beginTransaction();
Log.w("OpenHelper", "db.beginTransaction");
try {
// Create tables and test data
db.execSQL(CREATE_TABLE_FEEDS);
Log.w("OpenHelper", "execSQL");
db.setTransactionSuccessful();
Log.w("OpenHelper", "setTransactionSuccessful");
} catch (SQLException e) {
Log.e("Error creating tables and debug data", e.toString());
throw e;
} finally {
db.endTransaction();
}
} catch (Exception e) {
Log.e("OpenHelper", e.toString() );
} finally {
Log.w("OpenHelper", "out of onCreate");
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("OpenHelper", "Upgrading database, this will drop tables and recreate.");
onCreate(db);
}
}
}

现在每次“database = helper.getWritableDatabase();”称为应用程序崩溃并出现 NullPointerException。我设法将错误追溯到 OpenHelper 的 onCreate 方法和“db.beginTransaction();”行。

我错过了什么?

最佳答案

实际上您不需要在助手的 onCreate 方法中启动事务。尝试从方法中删除除 db.execSQL(CREATE_TABLE_FEEDS);

之外的所有内容

更新:我能够重复这个问题。

在该错误消失后,将 ​​fishDb 字段初始化移至 Activity 的 onCreate() 方法。在调用 onCreate 之前开始数据库初始化似乎不是一个好主意。

所以代替

fishDB fishDb = new fishDB(getApplicationContext());

fishDB fishDb;

onCreate(){
super.onCreate();
fishDb = new fishDB(getApplicationContext());
.... //init listener, query db etc..
}

关于android - 尝试调用 getWritableDatabase() 时继续获取 NullPointerExceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3489427/

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