请完成整个问题:
我想开发一个应用程序,我需要在Android中使用SQLite数据库。下面是显示的代码,它实现了数据库及其应用程序所需的所有操作以及随机数据(只是为了首先成功实现逻辑)。
此代码是用DBHelperDisplay.java编写的,由MainActivity.java通过intent调用。
package course.examples.jumboquest;
import java.util.Random;
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;
@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();
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);
final TextView 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");
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");
}
/* else{
// ans.setText("WRONG 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, 1);
}
@Override
public void onCreate(SQLiteDatabase db){
// TODO Auto-generated method stub
db.execSQL(
"CREATE TABLE Comics" +
"(id integer primary key, question text, choice1 text, choice2 text, choice3 text, choice4 text, answer text)");
}
@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(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int)DatabaseUtils.queryNumEntries(db,Table_Name);
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 where id = " + id + "", null);
return res;
}
}
我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.examples.jumboquest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DBHelperDisplay"></activity>
</application>
</manifest>
执行上面的代码时,我收到以下错误:
10-22 14:02:55.694: D/dalvikvm(2019): GC_FOR_ALLOC freed 99K, 6% free 3265K/3452K, paused 46ms, total 48ms
10-22 14:02:56.274: D/gralloc_goldfish(2019): Emulator without GPU emulation detected.
10-22 14:03:34.184: W/IInputConnectionWrapper(2019): showStatusIcon on inactive InputConnection
10-22 14:03:51.654: D/AndroidRuntime(2019): Shutting down VM
10-22 14:03:51.654: W/dalvikvm(2019): threadid=1: thread exiting with uncaught exception (group=0xb1a7dba8)
10-22 14:03:51.854: D/dalvikvm(2019): GC_FOR_ALLOC freed 129K, 6% free 3647K/3856K, paused 125ms, total 142ms
10-22 14:03:51.904: E/AndroidRuntime(2019): FATAL EXCEPTION: main
10-22 14:03:51.904: E/AndroidRuntime(2019): Process: course.examples.jumboquest, PID: 2019
10-22 14:03:51.904: E/AndroidRuntime(2019): java.lang.RuntimeException: Unable to start activity ComponentInfo{course.examples.jumboquest/course.examples.jumboquest.DBHelperDisplay}: android.database.sqlite.SQLiteException: Can't downgrade database from version 3 to 2
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.os.Looper.loop(Looper.java:136)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-22 14:03:51.904: E/AndroidRuntime(2019): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 14:03:51.904: E/AndroidRuntime(2019): at java.lang.reflect.Method.invoke(Method.java:515)
10-22 14:03:51.904: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-22 14:03:51.904: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-22 14:03:51.904: E/AndroidRuntime(2019): at dalvik.system.NativeStart.main(Native Method)
10-22 14:03:51.904: E/AndroidRuntime(2019): Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 3 to 2
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:361)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:255)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-22 14:03:51.904: E/AndroidRuntime(2019): at course.examples.jumboquest.DBHelperDisplay$DBHelper.insertQuestion(DBHelperDisplay.java:158)
10-22 14:03:51.904: E/AndroidRuntime(2019): at course.examples.jumboquest.DBHelperDisplay.onCreate(DBHelperDisplay.java:46)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.Activity.performCreate(Activity.java:5231)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-22 14:03:51.904: E/AndroidRuntime(2019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-22 14:03:51.904: E/AndroidRuntime(2019): ... 11 more
10-22 14:04:00.984: I/Process(2019): Sending signal. PID: 2019 SIG: 9
我在link
上发布了这个问题我得到你的回复卸载并再次安装该应用程序。虽然它删除了错误,但它给了我另一个错误:
10-22 07:43:43.644: D/dalvikvm(1955): GC_FOR_ALLOC freed 96K, 6% free 3265K/3448K, paused 42ms, total 48ms
10-22 07:43:44.164: D/gralloc_goldfish(1955): Emulator without GPU emulation detected.
10-22 07:43:52.554: D/AndroidRuntime(1955): Shutting down VM
10-22 07:43:52.554: W/dalvikvm(1955): threadid=1: thread exiting with uncaught exception (group=0xb1a7dba8)
10-22 07:43:52.684: E/AndroidRuntime(1955): FATAL EXCEPTION: main
10-22 07:43:52.684: E/AndroidRuntime(1955): Process: course.examples.jumboquest, PID: 1955
10-22 07:43:52.684: E/AndroidRuntime(1955): java.lang.RuntimeException: Unable to start activity ComponentInfo{course.examples.jumboquest/course.examples.jumboquest.DBHelperDisplay}: java.lang.NullPointerException
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.os.Looper.loop(Looper.java:136)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-22 07:43:52.684: E/AndroidRuntime(1955): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 07:43:52.684: E/AndroidRuntime(1955): at java.lang.reflect.Method.invoke(Method.java:515)
10-22 07:43:52.684: E/AndroidRuntime(1955): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-22 07:43:52.684: E/AndroidRuntime(1955): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-22 07:43:52.684: E/AndroidRuntime(1955): at dalvik.system.NativeStart.main(Native Method)
10-22 07:43:52.684: E/AndroidRuntime(1955): Caused by: java.lang.NullPointerException
10-22 07:43:52.684: E/AndroidRuntime(1955): at course.examples.jumboquest.DBHelperDisplay.onCreate(DBHelperDisplay.java:70)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.Activity.performCreate(Activity.java:5231)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-22 07:43:52.684: E/AndroidRuntime(1955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-22 07:43:52.684: E/AndroidRuntime(1955): ... 11 more
10-22 07:43:59.064: I/Process(1955): Sending signal. PID: 1955 SIG: 9
我尝试在Google上搜索并找到了Stack Overflow链接以解决同样的问题并获得了一些我尝试过的解决方案,但没有一个能够正常工作。此外,每次我在模拟器上再次运行应用程序进行更改时,我必须卸载它的先前版本,然后再次安装它。否则,它会向我显示相同的第一个错误,如果第一个错误得到纠正,那么它会显示第二个错误。
我尝试了很多东西,但没有任何工作。请帮我。我是Android中的SQLite新手。
答案 0 :(得分:0)
在创建表格查询
的末尾添加分号db.execSQL(
"CREATE TABLE Comics" +
"(id integer primary key, question text, choice1 text, choice2 text, choice3 text, choice4 text, answer text)");
替换为
db.execSQL(
"CREATE TABLE Comics" +
"(id integer primary key, question text, choice1 text, choice2 text, choice3 text, choice4 text, answer text);");
希望这至少可以用于创建表格。
其次,在插入值时,您应在String
Single Quotes
'Value'
个值