0

我在DBAdater.java类中尝试了这个查询:

 public Cursor getUserName(int userId) {

    Cursor c = db.query(REGISTER_TABLE, new String[] { REGISTER_ID,
            REGISTER_UN }, REGISTER_ID + " = " +userId,null,  null, null,
            null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

或者

public Cursor get_user(int id) {
    return db.query(REGISTER_TABLE,
            new String[] { REGISTER_ID, REGISTER_UN }, REGISTER_ID + "="

            + "'" + id + "'", null, null, null, null, null);
}

Test.java 获取用户名

Cursor clogin = db.getUserName(userId);
Log.i("user name  with repect to user id ", ""+clogin.getString(clogin.getColumnIndex(DBAdapter.RID)));

当我运行这个时,我得到这个错误:

04-30 14:18:52.925: E/AndroidRuntime(639): FATAL EXCEPTION: main
04-30 14:18:52.925: E/AndroidRuntime(639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.linkplus/com.android.Hello.test.test}: java.lang.NullPointerException
04-30 14:18:52.925: E/AndroidRuntime(639):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
......
......
04-30 14:18:52.925: E/AndroidRuntime(639): Caused by: java.lang.NullPointerException
04-30 14:18:52.925: E/AndroidRuntime(639):  at com.android.Hello.test.Profiles.getUserName(Profiles.java:63)
04-30 14:18:52.925: E/AndroidRuntime(639):  at com.android.Hello.test.Profiles.onCreate(Profiles.java:51)
04-30 14:18:52.925: E/AndroidRuntime(639):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

请指出此代码的错误是什么。提前致谢。

4

4 回答 4

1

尝试修改您的查询,例如

Cursor c = db.query(REGISTER_TABLE, new String[] { REGISTER_ID,
            REGISTER_UN }, REGISTER_ID + "=?", new String[]{userId},  null, null,
            null, null);

同样在text.java检查空光标

Cursor clogin = db.getUserName(userId);
if(clogin != null) {
Log.i("user name  with repect to user id ", ""+clogin.getString(clogin.getColumnIndex(DBAdapter.RID)));

}
于 2012-05-01T06:16:23.473 回答
0

显然查询没有返回任何值,它必须是数据库中的一些错误,或者很可能是查询字符串中的错误。

于 2012-04-30T08:50:28.837 回答
0

试试这个可能有效

 Cursor c = db.query(REGISTER_TABLE, 
                     new String[] { REGISTER_ID, REGISTER_UN }, 
                     REGISTER_ID + " = " +userId, 
                     null,
                     null, 
                     null, 
                     null);
于 2012-05-01T09:12:24.550 回答
0

取而代之的是,您可以使用此方法

Cursor c = db.rawQuery("Your Query without semicolon", selectionargs);

在你的情况下

Cursor c = db.rawQuery("select REGISTER_ID,REGISTER_UN from REGISTER_TABLE where REGISTER_ID="+userId,null);
于 2012-05-01T06:30:33.333 回答