我正在开发一个应用程序,通过它我可以存储和检索数据库中的数据。通过随机选择id(‘id’是表中受主键约束的列)、存储在Cursor对象中并与单选按钮一起显示来检索数据。然后,用户选择这些单选按钮中的一个,并相应地显示结果。
我将其实现为(代码: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为相同的。
记录Cat结果:
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
实施数据库的错误是什么?此外,早些时候它还显示了错误‘Cursor Index Requested-1 with Size 1’(请求的游标索引大小为1)。我做了一些更改,现在它显示上述错误。如果该错误再次出现,在前一种情况下也应该怎么做?我对Android中的SQLite数据库还很陌生。
3条答案
按热度按时间hs1ihplo1#
我强烈怀疑这是一个拼写错误,因为您的dbhelper_display.xml布局文件中没有包含
android:id="@+id/btClear
的Button。根据您能够编译的事实,我怀疑您在另一个文件中具有该ID,而不是在dbhelper_display.xml中。从您发布的XML代码中,我看到了以下内容:
Java代码是这样说的:
让这些保持一致,它就会奏效。还要检查一下你的
btnSubmit
,我认为它也有同样的问题。6ojccjat2#
这只是一个简单的拼写错误。您已在
Button btClear = (Button)findViewById(R.id.btClear);
中写入R.id.btClear
,您必须写入R.id.btnClear
:-/ttp71kqs3#
请看
Button btClear = (Button)findViewById(R.id.btClear);
行。如果它在该id上找不到视图,会发生什么?它将尝试将NULL转换为Button,然后将btClear设置为null
。然后尝试使用btClear.setText("CLEAR");
。您不能调用空对象上的方法,因此它会抛出NullPointerException。尝试查看这些行之间的
btClear
的值。它很可能是空的。编辑:在OP发布新信息后,它通知
btClear
实际上应该是btnClear
,如android:id="@+id/btnClear"
所描述的那样。这应该可以通过将R.id.btClear
更正为R.id.btnClear
来解决问题注意:我假设
**btClear.setText("CLEAR");
中的**
是您的问题格式中的一个拼写错误。