我的应用程序有时崩溃,有时它工作正常。我生成随机数,然后使用它们从数据库中检索数据,这些数字为id。有时候应用程序运行成功但有时候它会因上面提到的错误而崩溃。
gamedatabase.java
public String getquestion(int l) {
// TODO Auto-generated method stub
String[] columns = new String[] { key_rowid, key_ques, key_one,
key_two, key_three, key_four, key_answer };
Cursor c = ourDatabase.query(database_table, columns, key_rowid + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String qstn = c.getString(1);
return qstn;
}
return null;
}
public String getopt1(int l) {
// TODO Auto-generated method stub
String[] columns = new String[] { key_rowid, key_ques, key_one,
key_two, key_three, key_four, key_answer };
Cursor c = ourDatabase.query(database_table, columns, key_rowid + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String qstn = c.getString(2);
return qstn;
}
return null;
}
public String getopt2(int l) {
// TODO Auto-generated method stub
String[] columns = new String[] { key_rowid, key_ques, key_one,
key_two, key_three, key_four, key_answer };
Cursor c = ourDatabase.query(database_table, columns, key_rowid + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String opt1 = c.getString(3);
return opt1;
}
return null;
}
public String getopt3(int l) {
// TODO Auto-generated method stub
String[] columns = new String[] { key_rowid, key_ques, key_one,
key_two, key_three, key_four, key_answer };
Cursor c = ourDatabase.query(database_table, columns, key_rowid + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String opt3 = c.getString(4);
return opt3;
}
return null;
}
public String getopt4(int l) {
// TODO Auto-generated method stub
String[] columns = new String[] { key_rowid, key_ques, key_one,
key_two, key_three, key_four, key_answer };
Cursor c = ourDatabase.query(database_table, columns, key_rowid + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String opt4 = c.getString(5);
return opt4;
}
return null;
}
public String ans(int l) {
// TODO Auto-generated method stub
String[] columns = new String[] { key_rowid, key_ques, key_one,
key_two, key_three, key_four, key_answer };
Cursor c = ourDatabase.query(database_table, columns, key_rowid + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String answ = c.getString(6);
return answ;
}
return null;
}
public String getData() {
// TODO Auto-generated method stub
String[] coloumns = new String[] { key_rowid2, key_score };
Cursor c = ourDatabase.query(database_table2, coloumns, null, null,
null, null, null);
String result = "";
int irow = c.getColumnIndex(key_rowid2);
int iscore = c.getColumnIndex(key_score);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(irow) + " " + c.getString(iscore)
+ "\n";
}
return result;
}
public String getscore(int l) {
// TODO Auto-generated method stub
String[] coloumns = new String[] { key_rowid2, key_score };
Cursor c = ourDatabase.query(database_table2, coloumns, key_rowid2
+ "=" + l, null, null, null, null);
String result = "";
if (c != null) {
c.moveToFirst();
result = c.getString(1);
return result;
}
return null;
}
game.java
public class game extends Activity implements View.OnClickListener {
TextView tv1, tv2, tv4;
Button b1, b2, b3, b4;
String correctans;
Random ran=new Random();
int k;
int rand;
int score = 0;
int l, i = 0;
int[] no=new int[10];
//int no[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int b[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
//int no[] = { 20, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int rand=ran.nextInt(30);
int t = 0;
for(int k=0;k<10;k++)
{
if(k==0)
no[0]=rand;
else
{
while(t!=k)
{
t=1;
for(int j=0;j<k;j++)
{
if(no[j]==rand)
{
rand=ran.nextInt(30);
}
else
{
t++;
}
}
}
no[k]=rand;
}
}
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv4 = (TextView) findViewById(R.id.textView4);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
l = no[i];
getdata(l);
}
private void getdata(int l) {
// TODO Auto-generated method stub
if (i < 10) {
gamedatabase info = new gamedatabase(this);
info.open();
String question = info.getquestion(l);
String opt1 = info.getopt1(l);
String opt2 = info.getopt2(l);
String opt3 = info.getopt3(l);
String opt4 = info.getopt4(l);
correctans = info.ans(l);
info.close();
tv1.setText("Question " + (i + 1) + "/10");
tv2.setText(question);
tv2.setTextColor(Color.WHITE);
tv4.setText("YOUR SCORE IS " + score);
tv4.setTextColor(Color.LTGRAY);
b1.setText(opt1);
b2.setText(opt2);
// b2.setTextColor(color.darker_gray);
b3.setText(opt3);
// b3.setTextColor(color.darker_gray);
b4.setText(opt4);
// b4.setTextColor(color.darker_gray);
i++;
} else if (i > 9) {
final Context context = this;
Bundle basket = new Bundle();
basket.putInt("score", score);
Intent i = new Intent(context, score.class);
i.putExtras(basket);
startActivity(i);
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button1:
if (b[i] != 1) {
if (correctans.equals("a")) {
score = score + 10;
correctdial();
b[i] = 1;
} else if (correctans != "a") {
score = score - 5;
incorrectdial();
b[i] = 1;
}
} else {
dialog();
}
break;
case R.id.button2:
if (b[i] != 1) {
if (correctans.equals("b")) {
score = score + 10;
correctdial();
b[i] = 1;
} else if (correctans != "b") {
score = score - 5;
incorrectdial();
b[i] = 1;
}
} else {
dialog();
}
break;
case R.id.button3:
if (b[i] != 1) {
if (correctans.equals("c")) {
score = score + 10;
correctdial();
b[i] = 1;
} else if (correctans != "c") {
score = score - 5;
incorrectdial();
b[i] = 1;
}
} else {
dialog();
}
break;
case R.id.button4:
if (b[i] != 1) {
if (correctans.equals("d")) {
score = score + 10;
correctdial();
b[i] = 1;
} else if (correctans != "d") {
score = score - 5;
incorrectdial();
b[i] = 1;
}
} else {
dialog();
}
break;
}
}
@SuppressWarnings("deprecation")
private void dialog() {
// TODO Auto-generated method stub
AlertDialog alert3 = new AlertDialog.Builder(game.this).create();
alert3.setTitle("YOU HAVE ALREADY ANSWERED THAT QUESTION");
alert3.setMessage("PRESS CONTINUE TO MOE FORWARD");
alert3.setButton("CONTINUE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
if (i < 10) {
getdata(no[i]);
} else {
getdata(10);
}
}
});
alert3.show();
}
@SuppressWarnings("deprecation")
private void incorrectdial() {
// TODO Auto-generated method stub
AlertDialog alert2 = new AlertDialog.Builder(game.this).create();
alert2.setTitle("OOPS!!!!!!IT'S INCORRECT\nBETTER LUCK NEXT TIME");
alert2.setMessage("Your score is" + score);
alert2.setButton("CONTINUE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
if (i < 10) {
getdata(no[i]);
} else {
getdata(10);
}
}
});
alert2.show();
}
@SuppressWarnings("deprecation")
private void correctdial() {
// TODO Auto-generated method stub
AlertDialog alert = new AlertDialog.Builder(game.this).create();
alert.setTitle("YEAH!!!!!!!!THAT IS THE CORRECT ANSWER\nGREAT GOING");
alert.setMessage("Your score is" + score);
alert.setButton("CONTINUE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
if (i < 10) {
getdata(no[i]);
} else {
getdata(10);
}
}
});
alert.show();
}
}
Log Cat
08-12 08:52:52.995: I/Choreographer(1250): Skipped 51 frames! The application may be doing too much work on its main thread.
08-12 08:52:54.215: D/AndroidRuntime(1250): Shutting down VM
08-12 08:52:54.215: W/dalvikvm(1250): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-12 08:52:54.275: E/AndroidRuntime(1250): FATAL EXCEPTION: main
08-12 08:52:54.275: E/AndroidRuntime(1250): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-12 08:52:54.275: E/AndroidRuntime(1250): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
08-12 08:52:54.275: E/AndroidRuntime(1250): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-12 08:52:54.275: E/AndroidRuntime(1250): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-12 08:52:54.275: E/AndroidRuntime(1250): at com.example.fifaworldcupquiz.gamedatabase.getquestion(gamedatabase.java:174)
08-12 08:52:54.275: E/AndroidRuntime(1250): at com.example.fifaworldcupquiz.game.getdata(game.java:85)
08-12 08:52:54.275: E/AndroidRuntime(1250): at com.example.fifaworldcupquiz.game.access$0(game.java:79)
08-12 08:52:54.275: E/AndroidRuntime(1250): at com.example.fifaworldcupquiz.game$3.onClick(game.java:238)
08-12 08:52:54.275: E/AndroidRuntime(1250): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-12 08:52:54.275: E/AndroidRuntime(1250): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 08:52:54.275: E/AndroidRuntime(1250): at android.os.Looper.loop(Looper.java:137)
08-12 08:52:54.275: E/AndroidRuntime(1250): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-12 08:52:54.275: E/AndroidRuntime(1250): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 08:52:54.275: E/AndroidRuntime(1250): at java.lang.reflect.Method.invoke(Method.java:525)
08-12 08:52:54.275: E/AndroidRuntime(1250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-12 08:52:54.275: E/AndroidRuntime(1250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-12 08:52:54.275: E/AndroidRuntime(1250): at dalvik.system.NativeStart.main(Native Method)
希望有人可以提供帮助
答案 0 :(得分:1)
您的某些游标不包含任何结果行。
替换所有案例,例如
if (c != null) {
c.moveToFirst();
与
if (c.moveToFirst()) {
SQLiteDatabase
查询永远不会返回null
,您需要确保Cursor
在访问其数据之前指向有效行。在这种情况下,moveTo..()
会返回true。