Android - 如何从SQLite数据库处理程序传递String id

时间:2012-10-31 03:25:16

标签: java android sqlite

我想从SQLite数据库传递用户ID,但我很难搞清楚。 我一直在做我的研究,但由于我的Java技能水平很低,我不能再做任何进一步的动作了。请帮我解决这个问题,非常感谢。

这是errorLog

10-30 23:04:13.795: E/AndroidRuntime(22860): FATAL EXCEPTION: main
10-30 23:04:13.795: E/AndroidRuntime(22860): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.android/com.app.android.MainActivity}: java.lang.NullPointerException
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.os.Looper.loop(Looper.java:137)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.main(ActivityThread.java:4517)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.reflect.Method.invokeNative(Native Method)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.reflect.Method.invoke(Method.java:511)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at dalvik.system.NativeStart.main(Native Method)
10-30 23:04:13.795: E/AndroidRuntime(22860): Caused by: java.lang.NullPointerException
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at com.app.android.MainActivity.<init>(MainActivity.java:16)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.Class.newInstanceImpl(Native Method)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.Class.newInstance(Class.java:1319)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)

DatabaseHandler类

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_NAME = "app";
    private static final String TABLE_LOGIN = "login";

    private static final String KEY_ID = "id";  
    private static final String KEY_NAME = "name";  

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"          
                + KEY_NAME + " TEXT" + ")";
        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 void addUser(String id, String name) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, id); // id       
        values.put(KEY_NAME, name); // name     
        db.insert(TABLE_LOGIN, null, values);
        db.close(); 
    }

    public HashMap<String, String> getUserDetails(){
        HashMap<String,String> user = new HashMap<String,String>();
        String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        cursor.moveToFirst();
        if(cursor.getCount() > 0){
            user.put("id", cursor.getString(1));            
            user.put("name", cursor.getString(2));          

        }
        cursor.close();
        db.close();

        return user;
    }

    public int getUserCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();

        return rowCount;
    }

    public Cursor getUserID() {
        String qry = "SELECT id FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();     
        Cursor cursor = db.rawQuery(qry, null);
        return cursor;

    }

}

MainActivity Class

public class MainActivity extends Activity {

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Cursor id = db.getUserID();

        if(id.equals("0065")){

        Intent tab1 = new Intent(getApplicationContext(), RequestActivity.class);                                           
        tab1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(tab1);    

        finish();

        }else{

        Intent tab = new Intent(getApplicationContext(), ReceiveActivity.class);                
        tab.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(tab);

        finish();
    }

    }

}

1 个答案:

答案 0 :(得分:0)

我不认为错误确实存在。你有一个NullPointer。 logcat中的错误是由以下原因引起的:

DatabaseHandler db = new DatabaseHandler(getApplicationContext());

您应该在onCreate()

中初始化该db变量

无论如何,您返回ID的正确方法应该是:

public int getUserID() {
    String qry = "SELECT id FROM " + TABLE_LOGIN;
    SQLiteDatabase db = this.getReadableDatabase();     
    Cursor cursor = db.rawQuery(qry, null);
    int res = id.getInt(id.getColumnIndex("id"));
    cursor.close();
    return res;
}

(别忘了关闭光标