0

我正在制作一个安卓应用程序。并试图从列、名称和分数中获取数据

数据库表,但在运行时出现此错误,

抱歉,此应用程序已意外停止。

这是我的数据库类代码,

public long addscore(String name, int score)
{

    ContentValues values = new ContentValues();
    values.put(KEY_name, name); 
    values.put(KEY_score, score);

    // Inserting Row
    return db.insert(DATABASE_TABLE, null, values);
}
public Cursor getScore(long rowId) throws SQLException 
{
    Cursor mCursor = db.query(true,DATABASE_TABLE, new String[] {
            KEY_scoreid,KEY_name,KEY_score },,KEY_scoreid + "="
            + rowid,null,null,null,null,null);
    if (mCursor != null) 
    {
        mCursor.moveToFirst();
    }
    return mCursor;

}

这是我试图去的类的代码

从数据库表(Highscore.java)中获取数据,

public class Highscore extends Activity
{
TextView name1,name2,name3,name4,name5,score1,score2,score3,score4,score5;
DBAdapter db1;
Cursor c;
int id;
String n1,n2,n3,n4,n5;
String s1,s2,s3,s4,s5;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.highscore);
    Button backbtn=(Button)findViewById(R.id.backbtn);

    OnClickListener listener=new OnClickListener() 
    {
        public void onClick(View v) 
        {
            Intent i=new Intent(Highscore.this,MainActivity.class);
            startActivity(i);
            finish();
        }
    };
    backbtn.setOnClickListener(listener);
    id=1;
    name1=(TextView)findViewById(R.id.name1);
    score1=(TextView)findViewById(R.id.score1);



    db1=new DBAdapter(this);
    db1.open();

    c=db1.getScore(1);

        n1=c.getString(1);
        name1.setText(n1);
        s1=c.getString(2);
        score1.setText(s1);

        c=db1.getScore(2);
        n2=c.getString(1);
        name2.setText(n2);
        s2=c.getString(2);
        score2.setText(s2);

    c=db1.getScore(3);
        n3=c.getString(1);
        name3.setText(n3);
        s3=c.getString(2);
        score3.setText(s3);
        id++;   
    c=db1.getScore(4);
        n4=c.getString(1);
        name4.setText(n4);
        s4=c.getString(2);
        score4.setText(s4);
        id++;
    c=db1.getScore(5);
        n5=c.getString(1);
        name5.setText(n5);
        s5=c.getString(2);
        score5.setText(s5);

    c.deactivate();
    c.close();
    db1.close();
    }

}

这次我只取了 5 个名字和分数。

这是我的 Logcat 错误,

09-19 12:01:36.046: D/AndroidRuntime(455): Shutting down VM
09-19 12:01:36.046: W/dalvikvm(455): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-19 12:01:36.066: E/AndroidRuntime(455): FATAL EXCEPTION: main
09-19 12:01:36.066: E/AndroidRuntime(455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quizapp/com.example.quizapp.Highscore}: java.lang.NullPointerException
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.os.Looper.loop(Looper.java:123)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-19 12:01:36.066: E/AndroidRuntime(455):  at java.lang.reflect.Method.invokeNative(Native Method)
09-19 12:01:36.066: E/AndroidRuntime(455):  at java.lang.reflect.Method.invoke(Method.java:521)
09-19 12:01:36.066: E/AndroidRuntime(455):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-19 12:01:36.066: E/AndroidRuntime(455):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-19 12:01:36.066: E/AndroidRuntime(455):  at dalvik.system.NativeStart.main(Native Method)
09-19 12:01:36.066: E/AndroidRuntime(455): Caused by: java.lang.NullPointerException
09-19 12:01:36.066: E/AndroidRuntime(455):  at com.example.quizapp.Highscore.onCreate(Highscore.java:59)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 12:01:36.066: E/AndroidRuntime(455):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-19 12:01:36.066: E/AndroidRuntime(455):  ... 11 more

如果你们没有从我的问题中得到任何东西,所以请问.. 请帮我解决错误。

提前致谢

4

2 回答 2

0

从我看你的问题不在于数据库连接,你有一些未经处理的异常。您在 onCreate(Highscore.java:59) 中也有一个 NULL 指针异常

你可以做一件事:注释部分代码,直到你得到一个工作的应用程序(什么都不做),然后取消注释,直到你找到导致应用程序崩溃的确切代码,这很容易做到,通常你会找到那行有问题的

祝你好运

于 2013-09-19T06:45:58.180 回答
0

使用此代码插入您的.....

public Cursor getScore(long rowId) throws SQLException 

{

 Cursor mCursor = db.query(true,DATABASE_TABLE, new String[] {
            KEY_scoreid,KEY_name,KEY_score },KEY_scoreid + " ='"
            + rowid + "'",null,null,null,null,null);

  if (mCursor != null) 
    {
        mCursor.moveToFirst();
    }
    return mCursor;

}
于 2013-09-19T06:50:22.060 回答