我是学生并且第一次学习android并尝试使用SQLite制作测验应用程序它在模拟器上正常工作但在Android移动设备上崩溃
每当我尝试时,我都无法理解这个问题 安装测验通过复制它的.apk它安装在android上 电话,但每当我点击按钮显示问题它崩溃
这是我的db帮助程序类。
package com.example.quiz;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class dbhelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME="questio.db";
private final static int DATABASE_VERSION=2;
private final static String Android="quiz1";
private final static String php="quiz2";
private final static String ID="id";
private final static String score="score";
private final static String Question="question";
private final static String answer="ans";
private final static String op1="opa";
private final static String op2="opb";
private final static String op3="opc";
public static final String SQL_Create="create table " +Android+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+Question+" TEXT NOT NULL, "+answer+" TEXT,"+op1+" TEXT, "+op2+" TEXT, "+op3+" TEXT"+")";
public static final String SQL_Create2="create table " +php+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+Question+" TEXT NOT NULL, "+answer+" TEXT,"+op1+" TEXT, "+op2+" TEXT, "+op3+" TEXT"+")";
SQLiteDatabase db1;
public dbhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_Create);
db.execSQL(SQL_Create2);
db1=db;
db.close();
// TODO Auto-generated method stub
}
onUpgrade方法的所有语句都被注释掉了 表中插入的问题不会丢失。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//db.execSQL("drop table if exists"+Android);
//db.execSQL("drop table "+php);
//db.execSQL("drop table if exist"+TableName);
//onCreate(db);
// TODO Auto-generated method stubS
}
public void insertIntoTable(String str1,String str2,String str3,String str5,String str4)
{
db1=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Question, str1);
values.put(answer, str2);
values.put(op1, str3);
values.put(op2, str5);
values.put(op3, str4);
db1.insert(Android, null, values);
}
public void insertIntoTable2(String str1,String str2,String str3,String str4,String str5)
{
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Question, str1);
values.put(answer, str2);
values.put(op1, str3);
values.put(op2, str5);
values.put(op3, str4);
db1.insert(php, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " +Android;
//String del="delete from "+Android+ " where ID=16";
SQLiteDatabase db2=this.getReadableDatabase();
//db2.execSQL(del);
Log.d("chk", selectQuery);
Cursor cursor = db2.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
Log.d("quest", quest.getQUESTION());
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(5));
quest.setOPTC(cursor.getString(4));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + Android;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
答案 0 :(得分:1)
对于任何崩溃,请参阅logcat以获取有关查找问题的有用信息。始终在您的问题中包含堆栈跟踪。有关详情,请参阅Unfortunately MyApp has stopped. How can I solve this?。
在代码中,请勿在传递给您的帮助者close()
的{{1}}上致电SQLiteDatabase
。
为什么它&#34;工作&#34;在模拟器上是可能的,因为如果数据库文件已经存在(使用早期版本的代码设置),则不会调用onCreate()
回调。它只在刚刚创建数据库文件时调用。如果您发现崩溃,则会发生崩溃,例如在模拟器上卸载您的应用程序或清除其数据。有关详情,请参阅When is SQLiteOpenHelper onCreate() / onUpgrade() run?。