如果id是他们的表,我想获取行的id。但是在行Cursor cursorc = sqdb.rawQuery(strQue,null);之后运行应用程序获取空指针异常。有人可以帮我解决这个问题。
这是log cat。
10-13 16:34:22.340: E/AndroidRuntime(914): FATAL EXCEPTION: main
10-13 16:34:22.340: E/AndroidRuntime(914): java.lang.NullPointerException
10-13 16:34:22.340: E/AndroidRuntime(914): at com.explistview.Test_Demo.previousQuestionCalled(Test_Demo.java:254)
10-13 16:34:22.340: E/AndroidRuntime(914): at com.explistview.Test_Demo$1.onClick(Test_Demo.java:102)
10-13 16:34:22.340: E/AndroidRuntime(914): at android.view.View.performClick(View.java:2485)
10-13 16:34:22.340: E/AndroidRuntime(914): at android.view.View$PerformClick.run(View.java:9080)
10-13 16:34:22.340: E/AndroidRuntime(914): at android.os.Handler.handleCallback(Handler.java:587)
10-13 16:34:22.340: E/AndroidRuntime(914): at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 16:34:22.340: E/AndroidRuntime(914): at android.os.Looper.loop(Looper.java:123)
10-13 16:34:22.340: E/AndroidRuntime(914): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 16:34:22.340: E/AndroidRuntime(914): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 16:34:22.340: E/AndroidRuntime(914): at java.lang.reflect.Method.invoke(Method.java:507)
10-13 16:34:22.340: E/AndroidRuntime(914): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 16:34:22.340: E/AndroidRuntime(914): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 16:34:22.340: E/AndroidRuntime(914): at dalvik.system.NativeStart.main(Native Method)
这是活动代码。
button_Previouse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
previousQuestionCalled(v);
}
});
button_Next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nextQuestionCalled(v);
}
});
public void previousQuestionCalled(View view)
{
if (StaticClass.QuestionNumber > 0)
{
if (!StaticClass.isTest)
{
StaticClass.QuestionNumber = -1 + StaticClass.QuestionNumber;
reHitting2();
for(i=1 ; i <= vector_Size; i++)
{
String strQue = "select count(question_id) from question_answers where question_id =" + 'i';
System.out.println(" i = " + i);
Cursor cursorc = sqdb.rawQuery(strQue, null);
if (cursorc.moveToFirst())
{
int str_id = cursorc.getInt(cursorc.getColumnIndex("question_id"));
System.out.println("str_id = " + str_id);
}
}
if(i > 0)
{
String strUpdateQuery = "";
}
}
}
}
答案 0 :(得分:1)
在行Cursor cursorc = sqdb.rawQuery(strQue,null);获取空指针异常。
您尚未初始化sqdb
变量。
假设您正在使用SQLiteOpenHelper
,请在帮助程序上调用getReadbaleDatabase()
或getWritableDatabase()
以获取SQLiteDatabase
引用,您可以将其分配给sqdb
变量
我在E / CursorWindow(512)的log cat上有一行错误:字段槽0的错误请求,-1。 numRows = 1,numColumns = 1
列索引无效。变化
cursorc.getColumnIndex("question_id")
到例如。
0
检索游标中的第一列。您的getColumnIndex("question_id")
会返回-1
,因为count(question_id)
与question_id
不同。
答案 1 :(得分:-1)
更改代码
String strQue = "select count(question_id) from question_answers where question_id =" + 'i';
到
String strQue = "select count(question_id) from question_answers where question_id =" + i;
如果您的列question_id是整数类型
,否则
String strQue = "select count(question_id) from question_answers where question_id =" + "'i'";