Android - 尝试使用SQLite登录时收到错误

时间:2014-05-29 12:03:35

标签: java android sqlite

此处我已附上我的代码供您审核。

我的LoginActivity.java: -

login_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                u_name = user_name.getText().toString();
                p_word = password.getText().toString();
                System.out.println("u_name==> "+u_name);
                System.out.println("p_word==> "+p_word);
                test(u_name,p_word);
            }
        });

测试方法: -

public void test(String u_name,String p_word)
    {
        New_SQLController  dbHelper;
        dbHelper=new New_SQLController(LoginActivity.this); 
        if (dbHelper.Login(u_name,p_word))
        {
            Intent i = new Intent(LoginActivity.this,GPS_Example.class);
            startActivity(i);
        }
        else {
            System.out.println("Login Failed");
        }
    }

我的SQLController.java: -

public boolean Login(String username, String password) throws SQLException  
        {  
            Cursor mCursor = database.rawQuery("SELECT * FROM " + New_MyDbHelper.TABLE_LOGIN + " WHERE _user_id=? AND _password=?", new String[]{username,password});  
            if (mCursor != null) {  
                if(mCursor.getCount() > 0)  
                {  
                    return true;  
                }  
            }  
         return false;  
        }  

我的DBHelper.java: -

public class New_MyDbHelper extends SQLiteOpenHelper{   

        // TABLE INFORMATTION FOR LOGIN
             public static final String TABLE_LOGIN = "_login";
             public static final String LOGIN_PKEY_ID = "_id";
             public static final String USER_ID = "_user_id";
             public static final String PASSWORD = "_password";

             // DATABASE INFORMATION FOR LOGIN
             static final String DATA_BASE_NAME = "EXAMPLE.DB";
             static final int DATA_BASE_VERSION = 1;

            // TABLE CREATION STATEMENT FOR LOGIN
                 private static final String CREATE_ADDRESS_TABLE = "create table " + TABLE_LOGIN
                   + "(" + LOGIN_PKEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                   + USER_ID + " TEXT NOT NULL ," + PASSWORD + " TEXT NOT NULL);";


         public New_MyDbHelper(Context context) {
          super(context, DATA_BASE_NAME, null, DATA_BASE_VERSION);

         }

         @Override
         public void onCreate(SQLiteDatabase db) {
          db.execSQL(CREATE_ADDRESS_TABLE);
         }

         @Override
         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

          db.execSQL("DROP TABLE IF EXISTS " + CREATE_ADDRESS_TABLE);
          onCreate(db);

         }

}

我的问题是当我点击登录按钮时,我的应用程序被强制关闭。

错误讯息: -

05-29 11:23:31.271: E/AndroidRuntime(23372): FATAL EXCEPTION: main
05-29 11:23:31.271: E/AndroidRuntime(23372): java.lang.NullPointerException
05-29 11:23:31.271: E/AndroidRuntime(23372):    at com.testing.dbhelper.New_SQLController.Login(New_SQLController.java:50)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at com.testing.login_example.LoginActivity.test(LoginActivity.java:64)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at com.testing.login_example.LoginActivity$1.onClick(LoginActivity.java:40)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at android.view.View.performClick(View.java:4204)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at android.view.View$PerformClick.run(View.java:17355)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at android.os.Handler.handleCallback(Handler.java:725)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at android.os.Looper.loop(Looper.java:137)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at java.lang.reflect.Method.invokeNative(Native Method)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at java.lang.reflect.Method.invoke(Method.java:511)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-29 11:23:31.271: E/AndroidRuntime(23372):    at dalvik.system.NativeStart.main(Native Method)

请让我知道我做了什么错误?即使我尝试使用硬编码的值。它也显示错误。请让我解决这个问题。

提前致谢。

我的New_SQLController: -

public class New_SQLController {

    private New_MyDbHelper dbhelper;
    private Context ourcontext;
    private SQLiteDatabase database;

    public New_SQLController(Context c) {
        ourcontext = c;
    }

    public New_SQLController open() throws SQLException {
        dbhelper = new New_MyDbHelper(ourcontext);
        database = dbhelper.getWritableDatabase();
        return this;

    }

    public void close() {
        dbhelper.close();
    }

    public void insertData(String u_name, String p_word) {
        ContentValues cv = new ContentValues();
        cv.put(New_MyDbHelper.USER_ID, u_name);
        cv.put(New_MyDbHelper.PASSWORD, p_word);
        database.insert(New_MyDbHelper.TABLE_LOGIN, null, cv);
        //rowCount();   
    }

     public boolean validateUser(String username, String password){
         Cursor c = database.rawQuery(
                  "SELECT * FROM " + New_MyDbHelper.TABLE_LOGIN + " WHERE "
                          + New_MyDbHelper.USER_ID + "='" + username +"'AND "+New_MyDbHelper.PASSWORD+"='"+password+"'" ,  null);
         if (c.getCount()>0)
            return true;
            return false;
      }

     public boolean Login(String username, String password) throws SQLException  
        {  
            Cursor mCursor = database.rawQuery("SELECT * FROM " + New_MyDbHelper.TABLE_LOGIN + " WHERE _user_id=? AND _password=?", new String[]{username,password});  
            if (mCursor != null) {  
                if(mCursor.getCount() > 0)  
                {  
                    return true;  
                }  
            }  
         return false;  
        }  

    public void rowCount()
    {
        Cursor mCount= database.rawQuery("select count(*) from '"+New_MyDbHelper.TABLE_LOGIN+"'", null);
        mCount.moveToFirst();
        int count= mCount.getInt(0);
        mCount.close();
        System.out.println("Totoal Row Count==>"+count);
        if(count>5)
            deleteFirstRow();
    }

    public void deleteAll() {
        database.execSQL("delete from " + New_MyDbHelper.TABLE_LOGIN);
        database.close();
    }

    public void deleteFirstRow() {
        Cursor cursor = database.query(New_MyDbHelper.TABLE_LOGIN, null, null,
                null, null, null, null);

        if (cursor.moveToFirst()) {
            String rowId = cursor.getString(cursor
                    .getColumnIndex(New_MyDbHelper.LOGIN_PKEY_ID));

            database.delete(New_MyDbHelper.TABLE_LOGIN, New_MyDbHelper.LOGIN_PKEY_ID
                    + "=?", new String[] { rowId });
        }
        database.close();
    }

    public Cursor readEntry() {

        String[] allColumns = new String[] { New_MyDbHelper.LOGIN_PKEY_ID,
                New_MyDbHelper.USER_ID, New_MyDbHelper.PASSWORD };

        Cursor c = database.query(New_MyDbHelper.TABLE_LOGIN, allColumns, null,
                null, null, null, null);

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

    }


}

0 个答案:

没有答案