gpt4 book ai didi

Android [IllegalArgumentException : the bind value at index 1 is null] for no apparent reason

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:29 26 4
gpt4 key购买 nike

当从两个不同的 Activity 引用时,我试图让一些与数据库有关的方法工作,这导致我在同一类中使用不同的上下文重新初始化整个数据库。它适用于一个 Activity ,但是当数据库在另一个 Activity 中关闭、初始化和打开时,它会导致 IllegalArgumentException。这对我来说毫无意义,因为我没有更改任何直接连接到数据库的方法,并且当代码被分成两个类时它可以完美地工作。

堆栈跟踪:

05-09 20:44:42.661: E/AndroidRuntime(953): FATAL EXCEPTION: main
05-09 20:44:42.661: E/AndroidRuntime(953): java.lang.RuntimeException: Unable to resume activity {maturaarbeit.nicola_pfister.marks/maturaarbeit.nicola_pfister.marks.Marks}: java.lang.IllegalArgumentException: the bind value at index 1 is null
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.os.Looper.loop(Looper.java:137)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-09 20:44:42.661: E/AndroidRuntime(953): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:44:42.661: E/AndroidRuntime(953): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:44:42.661: E/AndroidRuntime(953): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-09 20:44:42.661: E/AndroidRuntime(953): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-09 20:44:42.661: E/AndroidRuntime(953): at dalvik.system.NativeStart.main(Native Method)
05-09 20:44:42.661: E/AndroidRuntime(953): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
05-09 20:44:42.661: E/AndroidRuntime(953): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
05-09 20:44:42.661: E/AndroidRuntime(953): at maturaarbeit.nicola_pfister.marks.database.DBAdapter.getAverage(DBAdapter.java:200)
05-09 20:44:42.661: E/AndroidRuntime(953): at maturaarbeit.nicola_pfister.marks.MyMenu.getData(MyMenu.java:71)
05-09 20:44:42.661: E/AndroidRuntime(953): at maturaarbeit.nicola_pfister.marks.Marks.onResume(Marks.java:52)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.Activity.performResume(Activity.java:5082)
05-09 20:44:42.661: E/AndroidRuntime(953): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
05-09 20:44:42.661: E/AndroidRuntime(953): ... 12 more

Marks(menu.onResume)中的异常抛出方法:

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

MyMenu menu = new MyMenu(this);

ListView lView;
View view;

private String TAG = "Marks";
@Override
protected void onResume() {
super.onResume();
menu.closedb();
menu.opendb(TAG);
menu.getData(lView, view);

}

MyMenu中涉及的方法(db.open()异常):

DBAdapter db;

public void opendb(String caller) {
db = new DBAdapter(context);
this.caller = caller; //Is used to distinguish which activity called the class; not involved in this problem
db.open();

}

public void closedb() {
db.clean();
db.close();
}

@SuppressWarnings("deprecation")
public String getData(final ListView lView, final View view) {
SimpleCursorAdapter adapter = null;
if (caller == "Main") {
adapter = new SimpleCursorAdapter(context,
android.R.layout.simple_list_item_activated_1,
db.getAllSubjects(),
new String[] { "subject" },
new int[] { android.R.id.text1 });
} else if (caller == "Marks") {
adapter = new SimpleCursorAdapter(context,
android.R.layout.simple_list_item_activated_1,
db.getMarks(subject),
new String[] {"value"},
new int[] { android.R.id.text1 });

Cursor cursor = db.getAverage(selection);
average = cursor.getDouble(cursor.getColumnIndexOrThrow(DBAdapter.KEY_VALUE));

if (average != 0.0) {
TextView text = new TextView(context);
text = (TextView)view.findViewById(R.id.marks_average);
text.setText(context.getString(R.string.average) + " " + average);
text.setBackgroundColor(context.getResources().getColor(R.color.lightgrey));
} else {
TextView text = new TextView(context);
text = (TextView)view.findViewById(R.id.marks_average);
text.setText("");
text.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
}
}

DBAdapter 中涉及的方法(在一个 Activity 中工作正常,在 getAverage() 上抛出异常):

public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}

public void close() {
DBHelper.close();
}

public boolean clean () {
db.delete(DATABASE_TABLE_SUBJECTS, KEY_SUBJECT +"=?", new String[] {""});
db.delete(DATABASE_TABLE_MARKS, KEY_VALUE + "=?", new String[] {""});
return true;
}

public Cursor getAverage(String subject) throws SQLException {
Cursor mCursor =
db.query(true, DATABASE_TABLE_AVERAGE, new String[] {
KEY_ROWID,
KEY_SUBJECT,
KEY_VALUE
},
KEY_SUBJECT + "=?",
new String[] {subject}, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

我最初认为错误出在 open() 方法中,但在查看堆栈跟踪时我发现异常是由 getAverage() 抛出的,这让我很困惑,因为此时甚至没有调用它。我假设在 Marks Activity 中正确初始化 DBAdapter 类缺少一些东西,但找不到罪魁祸首。如果您需要更多信息或代码来解决这个难题,请随时告诉我。非常感谢您的帮助!

最佳答案

我可以看看 menu.getData(lView, view) 方法吗?

无论如何,我建议不要从 Database Helper 类返回 Cursor 对象。为什么?因为,可能有多个调用,可能是对数据库的并行调用。每次你需要从数据库中获取一些东西时,你都需要打开它,然后关闭它。如果您的应用程序的一部分将关闭数据库,而另一部分尚未完成其工作 - 并且将尝试从数据库中的数据访问某些内容或尝试从 Cursor 对象获取数据 - 它肯定会崩溃。

我的解决方案是制作一些应用程序模型<=> 数据库模型对象,它将双向映射数据。

例如:

public LinkedList<Person> getPersonList(){
Cursor result = db.query(...);

LinkedList<Person> people = null;

if(result.moveToFirst()){
people = new LinkedList<Person>();
Person person = null;

do{
person = new Person();

person.setBirthDate(result.getInt(0));
/*some more attributes mapping*/

people.add(person);
}while(result.hasNext());
}

return people;
}

关于Android [IllegalArgumentException : the bind value at index 1 is null] for no apparent reason,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471410/

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