Java android如何在android的Asyntask中加载sqlite数据

Java android如何在android的Asyntask中加载sqlite数据,java,android,sqlite,Java,Android,Sqlite,我不熟悉java和android。我想在asyntask类中获取sqlite行,但它返回NullpointerException 我不知道如何传递钥匙。谁能告诉我实现这一目标的正确方法是什么 类数据库 public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION=1; private static final String DATABASE_NAME="cloud

我不熟悉java和android。我想在asyntask类中获取sqlite行,但它返回NullpointerException 我不知道如何传递钥匙。谁能告诉我实现这一目标的正确方法是什么

类数据库

public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="cloud_contacts";
public static final String TABLE_LOGIN="login";
public static final String KEY_ID="id";
private static final String KEY_USERNAME="uname";
private static final String KEY_PASSWORD="password";
public static final String KEY_USERNAME_DEVICE="unamedevice";
public static final String KEY_PASSWORD_DEVICE="passworddevice";
private static final String CREATE_LOGIN_TABLE="CREATE TABLE "+TABLE_LOGIN + " ("
        +KEY_ID+" INTEGER PRIMARY KEY, "
        +KEY_USERNAME + " TEXT, "
        +KEY_PASSWORD + " TEXT, "
        +KEY_USERNAME_DEVICE + " TEXT, "
        +KEY_PASSWORD_DEVICE+" TEXT"+ ")";
public  DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


/**
 * tao bang sql user
 *
 */
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_LOGIN_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
    onCreate(db);
}

public  String getKeyUsernameDevice(int id) {
    String infor = null;
     SQLiteDatabase db=DatabaseHandler.this.getWritableDatabase();
    Cursor cursor =  db.rawQuery("select " + KEY_USERNAME_DEVICE + " from  " + TABLE_LOGIN + " where " + KEY_ID
            + " =? ", new String[]{String.valueOf(1)});
    if (cursor != null) {
        // move cursor to first row
        if (cursor.moveToFirst()) {
            do {
                // Get version from Cursor
                infor = cursor.getString(cursor.getColumnIndex(KEY_USERNAME_DEVICE));
                // add the bookName into the bookTitles ArrayList
                // move to next row
            } while (cursor.moveToNext());
        }
    }
    return infor;
}
public  String getKeyPasswordDevice(int id) {
    String infor = null;
    SQLiteDatabase db=DatabaseHandler.this.getWritableDatabase();
    Cursor cursor =  db.rawQuery("select " + KEY_PASSWORD_DEVICE + " from  " + TABLE_LOGIN + " where " + KEY_ID
            + " =? ", new String[]{String.valueOf(1)});
    if (cursor != null) {
        // move cursor to first row
        if (cursor.moveToFirst()) {
            do {
                // Get version from Cursor
                infor = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD_DEVICE));
                // add the bookName into the bookTitles ArrayList
                // move to next row
            } while (cursor.moveToNext());
        }
    }
    return infor;
}
类包含Asyntask

   public  class MyCommandTask extends AsyncTask<String,Void,Document>
{
    String username;
    String password;
    DatabaseHandler databaseHandler;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Document doInBackground(String... params) {
        InputStream inputStream = null;
        HttpURLConnection urlConnection = null;
        try {
            this.databaseHandler=new DatabaseHandler(getApplicationContext());
            username=databaseHandler.getKeyUsernameDevice(1);
            password="admin";
            /* forming th java.net.URL object */
            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            String basicAuth ="Basic " + Base64.encodeToString((username + ":" + password).getBytes(),Base64.NO_WRAP);
            urlConnection.setRequestProperty("Authorization", basicAuth);
            /* for Get request */
            urlConnection.setRequestMethod("GET");
            inputStream =urlConnection.getInputStream();
            /* 200 represents HTTP OK */
            Document responseXML = parseXmlDom(inputStream);
            return responseXML;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
       return null;

    }

我怀疑问题出在游标上。getStringcol_索引,请尝试以下操作:

public  String getKeyUsernameDevice(int id) {
    String infor = null;
    SQLiteDatabase db=DatabaseHandler.this.getWritableDatabase();
    Cursor cursor =  db.rawQuery("select " + KEY_USERNAME_DEVICE + " from  " + TABLE_LOGIN + " where " + KEY_ID
            + " =? ", new String[]{String.valueOf(id)});

    // move cursor to first row
    if (cursor.moveToFirst()) {
        // Get version from Cursor
        infor = cursor.getString(0);
    }
    return infor;
}

由于您的查询是选择用户名。。。但是不要选择*所以现在查询用户名的列索引应该是第一列0。

首先要提高性能,不要在游标的所有迭代中搜索相同的列索引

你应该喜欢:

if (cursor != null) {
    Integer colIndex = cursor.getColumnIndex(KEY_USERNAME_DEVICE);
    // move cursor to first row
    if (cursor.moveToFirst()) {
        do {
            // Get version from Cursor
            infor = cursor.getString( colIndex );
            // add the bookName into the bookTitles ArrayList
            // move to next row
        } while (cursor.moveToNext());
    }
}

那么,您能给我们看一下您的logCat以确定NullpointerException在哪里吗

你能试着全部查询,看看数据是否存在吗?Cursor Cursor=db.rawQueryselect+KEY\u USERNAME\u DEVICE+from+TABLE\u LOGIN;请张贴完整的日志。