我正在制作一个基于测验的应用程序,在这里我想根据subectid
该表中给出的字段从数据库表中获取问题。
我在 Database 类中使用了以下代码来获取数据,
public Cursor getText(long subId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_id, KEY_ques,KEY_correctans,KEY_wrongans1,KEY_wrongans2,KEY_wrongans3,KEY_subject,KEY_subjectid },
KEY_subjectid + "=" + subId, null,null,null, null,null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
下面是我在课堂上使用的代码,我从表中获取数据,
public void showdata()
{
quesno.setText(Integer.toString(count));
tvscore.setText(Integer.toString(score));
db=new DBAdapter(this);
db.open();
c=db.getText(subid); \\Suppose Here subid=1
if(c.getCount()> 0)
{
String ques=c.getString(1);
tv.setText(ques);
cans=c.getString(2);
shuffleArray(ans);
int[] a = new int[4];
int j=0;
for (int i = 0; i < ans.length; i++)
{
a[j]=ans[i];
j++;
}
ans1=c.getString(a[0]);
ans2=c.getString(a[1]);
ans3=c.getString(a[2]);
ans4=c.getString(a[3]);
rb1.setText(ans1);
rb2.setText(ans2);
rb3.setText(ans3);
rb4.setText(ans4);
}
count++;
}
当我使用此代码时,我收到Force close
错误并低于 Logcat 错误,
09-25 18:14:33.011: D/AndroidRuntime(521): Shutting down VM
09-25 18:14:33.011: W/dalvikvm(521): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-25 18:14:33.031: E/AndroidRuntime(521): FATAL EXCEPTION: main
09-25 18:14:33.031: E/AndroidRuntime(521): java.lang.NullPointerException
09-25 18:14:33.031: E/AndroidRuntime(521): at com.example.quizapp.Firstques$2.onClick(Firstques.java:99)
09-25 18:14:33.031: E/AndroidRuntime(521): at android.view.View.performClick(View.java:2408)
09-25 18:14:33.031: E/AndroidRuntime(521): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
09-25 18:14:33.031: E/AndroidRuntime(521): at android.view.View$PerformClick.run(View.java:8816)
09-25 18:14:33.031: E/AndroidRuntime(521): at android.os.Handler.handleCallback(Handler.java:587)
09-25 18:14:33.031: E/AndroidRuntime(521): at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 18:14:33.031: E/AndroidRuntime(521): at android.os.Looper.loop(Looper.java:123)
09-25 18:14:33.031: E/AndroidRuntime(521): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-25 18:14:33.031: E/AndroidRuntime(521): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 18:14:33.031: E/AndroidRuntime(521): at java.lang.reflect.Method.invoke(Method.java:521)
09-25 18:14:33.031: E/AndroidRuntime(521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-25 18:14:33.031: E/AndroidRuntime(521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-25 18:14:33.031: E/AndroidRuntime(521): at dalvik.system.NativeStart.main(Native Method)
09-25 18:14:39.672: I/Process(521): Sending signal. PID: 521 SIG: 9
希望我的问题对所有人都很清楚,如果没有,请询问。
提前致谢。