gpt4 book ai didi

java - SQLite 数据库中第 71 行的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:38:25 25 4
gpt4 key购买 nike

我正在开发一个应用程序,用于从数据库存储和检索数据。通过随机选择id(“id”是表中的主键约束列)来检索数据,存储在光标对象中并与单选按钮一起显示。然后,用户选择这些单选按钮之一,并相应地显示结果。

我已将其实现为(代码:DBHelperDisplay.java):

package course.examples.jumboquest;

import java.util.ArrayList;
import java.util.Random;
//import java.io.*;
//import java.lang.*;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
//import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
//import android.content.Intent;
import android.os.CountDownTimer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

public class DBHelperDisplay extends ActionBarActivity {

TextView tv;
DBHelper myDB;
RadioGroup radioChoices;
RadioButton rbtChoice;
Button btSubmit;
String choice1;
String choice2;
String choice3;
String choice4;
String strAns;
CustomTimer cdt;
TextView quest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbhelper_display);

cdt = new CustomTimer(20000, 1000);
cdt.start();

myDB = new DBHelper(this);

myDB.insertQuestion(1, "who is the team member whose name starts with s?", "Vinita", "Akanksha", "Swati", "Megha", "Swati");
myDB.insertQuestion(2, "who is the team member whose name starts with m?", "Vinita", "Akanksha", "Swati", "Megha", "Megha");
myDB.insertQuestion(3, "who is the team member whose name starts with a?", "Vinita", "Akanksha", "Swati", "Megha", "Akanksha");
myDB.insertQuestion(4, "who is the team member whose name starts with v?", "Vinita", "Akanksha", "Swati", "Megha", "Vinita");
myDB.insertQuestion(5, "who is the team member whose name ends with i?", "Vinita", "Akanksha", "Swati", "Megha", "Swati");

Cursor rs = myDB.getData();
rs.moveToFirst();
String Question = rs.getString(rs.getColumnIndex(DBHelper.Col_Ques));
choice1 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice1));
choice2 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice2));
choice3 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice3));
choice4 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice4));
strAns = rs.getString(rs.getColumnIndex(DBHelper.Col_Ans));


tv = (TextView) findViewById(R.id.timertxt);

quest = (TextView) findViewById(R.id.quest);
quest.setText(Question);

//final TextView ans = (TextView) findViewById(R.id.ans);

Button btClear = (Button)findViewById(R.id.btClear);
btClear.setText("CLEAR"); // Line 71

addListenerRadioChoices() ;

btClear.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//ans.setText("");

}
});
}
public void addListenerRadioChoices(){

radioChoices = (RadioGroup) findViewById(R.id.radioChoices);

((RadioButton) radioChoices.getChildAt(0)).setText(choice1);
((RadioButton) radioChoices.getChildAt(1)).setText(choice2);
((RadioButton) radioChoices.getChildAt(2)).setText(choice3);
((RadioButton) radioChoices.getChildAt(3)).setText(choice4);

btSubmit = (Button)findViewById(R.id.btSubmit);
btSubmit.setText("SUBMIT");
btSubmit.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {

int selected = radioChoices.getCheckedRadioButtonId();
rbtChoice = (RadioButton) findViewById(selected);
String Ans = rbtChoice.getText().toString();
if(Ans.equalsIgnoreCase(strAns)){

cdt.cancel();
//ans.setText(" ANSWER");

}


}
});
}

public class CustomTimer extends CountDownTimer{
//TextView ed;
public CustomTimer(long millisInFuture, long countDownInterval){
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished){
//current = millisUntilFinished/1000;
tv.setText("Time Left:" + millisUntilFinished/1000);
}
@Override
public void onFinish() {
tv.setText("Time Up - lost the game!");
}
}

public class DBHelper extends SQLiteOpenHelper {

public static final String Database_Name = "Questions.db";
public static final String Table_Name = "Comics";
public static final String Col_ID = "id";
public static final String Col_Ques = "question";
public static final String Col_Choice1 = "choice1";
public static final String Col_Choice2 = "choice2";
public static final String Col_Choice3 = "choice3";
public static final String Col_Choice4 = "choice4";
public static final String Col_Ans = "answer";

public DBHelper(Context context){
super(context, Database_Name, null, 4);
}
@Override
public void onCreate(SQLiteDatabase db){
// TODO Auto-generated method stub
String CREATE_TABLE = "CREATE TABLE " + Table_Name + "( " + Col_ID + " INTEGER PRIMARY KEY, " + Col_Ques + " TEXT, " + Col_Choice1 + " TEXT, "
+ Col_Choice2 + " TEXT, " + Col_Choice3 + " TEXT, " + Col_Choice4 + " TEXT, " + Col_Ans + " TEXT );" ;
db.execSQL(CREATE_TABLE);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS Comics");
onCreate(db);
}

public boolean insertQuestion(int id, String question, String choice1, String choice2, String choice3, String choice4, String answer){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put("id",id);
contentValues.put("question",question);
contentValues.put("choice1",choice1);
contentValues.put("choice2",choice2);
contentValues.put("choice3",choice3);
contentValues.put("choice4",choice4);
contentValues.put("answer",answer);
db.insert("Comics", null, contentValues);
db.close();
return true;
}
public Cursor getData(){
//public void getData(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int)DatabaseUtils.queryNumEntries(db,Table_Name);
//System.out.println(numRows);
int min = 1;
int max = numRows;
Random r = new Random();
int id = r.nextInt(max - min + 1) + min;
// Cursor res= db.rawQuery("Select * from Comics", null);
// ArrayList<String> array_list = new ArrayList<String>();
Cursor res= db.rawQuery("Select * from Comics where id = '" + id + "'", null);
/*res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(Col_ID)));
res.moveToNext();
}
//quest.setText("");
StringBuilder text = new StringBuilder();
for(int i= 0; i<array_list.size();i++){
text.append(array_list.get(i).toString() + "\n");
}
quest.setText(text.toString());*/
return res;
}
}
}

我的dbhelper_display.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/timertxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/quest"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioGroup
android:id="@+id/radioChoices"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/rbtChoice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/rbtChoice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioButton
android:id="@+id/rbtChoice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/rbtChoice4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RadioGroup>

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/ans"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

我在实现上述代码时遇到 NullPointerException。我已附上相同的 LOgCat。

记录猫结果:

10-24 07:57:44.567: D/dalvikvm(1498): GC_FOR_ALLOC freed 91K, 5% free 3265K/3432K, paused 80ms, total 81ms
10-24 07:57:45.107: D/gralloc_goldfish(1498): Emulator without GPU emulation detected.
10-24 07:57:51.387: D/AndroidRuntime(1498): Shutting down VM
10-24 07:57:51.387: W/dalvikvm(1498): threadid=1: thread exiting with uncaught exception (group=0xb1a47ba8)
10-24 07:57:51.437: E/AndroidRuntime(1498): FATAL EXCEPTION: main
10-24 07:57:51.437: E/AndroidRuntime(1498): Process: course.examples.jumboquest, PID: 1498
10-24 07:57:51.437: E/AndroidRuntime(1498): java.lang.RuntimeException: Unable to start activity ComponentInfo{course.examples.jumboquest/course.examples.jumboquest.DBHelperDisplay}: java.lang.NullPointerException
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.os.Handler.dispatchMessage(Handler.java:102)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.os.Looper.loop(Looper.java:136)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-24 07:57:51.437: E/AndroidRuntime(1498): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 07:57:51.437: E/AndroidRuntime(1498): at java.lang.reflect.Method.invoke(Method.java:515)
10-24 07:57:51.437: E/AndroidRuntime(1498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-24 07:57:51.437: E/AndroidRuntime(1498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-24 07:57:51.437: E/AndroidRuntime(1498): at dalvik.system.NativeStart.main(Native Method)
10-24 07:57:51.437: E/AndroidRuntime(1498): Caused by: java.lang.NullPointerException
10-24 07:57:51.437: E/AndroidRuntime(1498): at course.examples.jumboquest.DBHelperDisplay.onCreate(DBHelperDisplay.java:71)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.Activity.performCreate(Activity.java:5231)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-24 07:57:51.437: E/AndroidRuntime(1498): ... 11 more
10-24 07:57:55.767: I/Process(1498): Sending signal. PID: 1498 SIG: 9

执行数据库时出现什么错误?此外,早些时候它还显示错误“游标索引请求 -1,大小为 1”。我做了一些更改,现在显示上述错误。如果同样的错误再次出现,应该怎么做?我是 Android 中的 SQLite 数据库新手。

最佳答案

我强烈怀疑这是一个拼写错误,您的 dbhelper_display.xml 布局文件中没有带有 android:id="@+id/btClear 的按钮。基于事实如果您能够编译,我怀疑您在不同的文件中拥有该 ID,但在 dbhelper_display.xml 中没有。

从您发布的 XML 代码中,我看到了这一点:

android:id="@+id/btnClear"

Java 代码是这样说的:

Button btClear = (Button)findViewById(R.id.btClear);

使这些保持一致,它就会起作用。另请检查您的 btnSubmit,我相信它也有同样的问题。

关于java - SQLite 数据库中第 71 行的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26548969/

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