活动不会使用本地数据库进行身份验证

时间:2012-08-25 04:08:52

标签: android sqlite nullpointerexception logcat onclicklistener

我遇到问题的程序是一个登录页面,它使用sqlite数据库进行身份验证,并使用注册页面将数据插入数据库。我能够将数据插入数据库,但我的登录类没有从dn中提取信息进行身份验证加上我没有收到任何错误。

我在我的登录课程中放了一个printstackrtace(),但我不确定这些信息实际上在说什么。

db class:

     public class LoginDB extends SQLiteOpenHelper {



//Table attributes
public static final String DATABASE_NAME = "logindatadb";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME_CREDENTIALS = "credentials";

// Data attributes
public static final String COLUMN_NAME_USERNAME = "username";
public static final String COLUMN_NAME_PASSWORD = "passcode";

private static SQLiteOpenHelper DBHelper;
private static SQLiteDatabase db;



public LoginDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub      

        String sqlDataStore = "create table if not exists " +
        TABLE_NAME_CREDENTIALS + " ("+ BaseColumns._ID + " integer primary key autoincrement,"

                    + COLUMN_NAME_USERNAME + " text not null,"
                    + COLUMN_NAME_PASSWORD + " text not null);";

        db.execSQL(sqlDataStore);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if(oldVersion == 1 && newVersion == 2){
            //Upgrade the database
    }   

}

    public static boolean Login(String username, String password) {
        Cursor c =  db.rawQuery(
                "SELECT * FROM " + DATABASE_NAME + " WHERE "
                        + COLUMN_NAME_USERNAME + " = '" + username +"' AND "+ COLUMN_NAME_PASSWORD +" = '"+ password +"'" ,  null);
       if (c.getCount()>0)
          return true;
          return false;
    }

    public void open() {
        // TODO Auto-generated method stub
        db = DBHelper.getWritableDatabase();
    }
    public void close() {
        db.close();
    }

}

注册:

    private void insertCredentials(RegDetails regDetails){

        LoginDB androidOpenDBHelper = new LoginDB(this);

        SQLiteDatabase sqliteDB = androidOpenDBHelper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        contentValues.put(LoginDB.COLUMN_NAME_USERNAME, rUsername);
        contentValues.put(LoginDB.COLUMN_NAME_PASSWORD, rPasscode);

        long affectedColumnid = sqliteDB.insert(LoginDB.TABLE_NAME_CREDENTIALS, null, contentValues);

        Toast.makeText(getApplicationContext(), "Credentials Saved! Please login" + affectedColumnid, Toast.LENGTH_SHORT).show();

        sqliteDB.close();
        finish();
    }   

  }

登录:

            lsLogin.setOnClickListener (new OnClickListener() {

                @Override
                public void onClick(View v) {
                    //check login
                    String username = lsUsername.getText().toString();
                    String password = lsPassword.getText().toString();
                    try{
                        if(username.length()>0 && password.length()>0)
                        {
                            LoginDB dbUser = new LoginDB(LoginScrExample.this);
                            dbUser.open();

                        if(LoginDB.Login(username, password)) {
                            Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
                            startActivity(goToNextActivity);

                                Toast.makeText(LoginScrExample.this,"Login Sucessful",Toast.LENGTH_LONG).show();
                            }else{
                                Toast.makeText(LoginScrExample.this,"Invalid Username/Password",Toast.LENGTH_LONG).show();
                            }
                        dbUser.close();
                            }
                    }
                    catch(Exception err){
                        err.printStackTrace();
                        Toast.makeText(LoginScrExample.this," Something is wrong " ,Toast.LENGTH_LONG).show();

                    }

                    }
                       {

                       }    

                                            }
                            );

尝试进行身份验证时的Logcat:

     08-23 14:34:58.713: W/System.err(1396): java.lang.NullPointerException
     08-23 14:34:58.733: W/System.err(1396):    at com.LoginScr.Example.LoginDB.open(LoginDB.java:68)
     08-23 14:34:58.733: W/System.err(1396):    at com.LoginScr.Example.LoginScrExample$1.onClick(LoginScrExample.java:53)
     08-23 14:34:58.733: W/System.err(1396):    at android.view.View.performClick(View.java:3511)
     08-23 14:34:58.733: W/System.err(1396):    at android.view.View$PerformClick.run(View.java:14105)
     08-23 14:34:58.753: W/System.err(1396):    at android.os.Handler.handleCallback(Handler.java:605)
     08-23 14:34:58.753: W/System.err(1396):    at android.os.Handler.dispatchMessage(Handler.java:92)
     08-23 14:34:58.763: W/System.err(1396):    at android.os.Looper.loop(Looper.java:137)
     08-23 14:34:58.775: W/System.err(1396):    at android.app.ActivityThread.main(ActivityThread.java:4424)
     08-23 14:34:58.775: W/System.err(1396):    at java.lang.reflect.Method.invokeNative(Native Method)
     08-23 14:34:58.775: W/System.err(1396):    at java.lang.reflect.Method.invoke(Method.java:511)
     08-23 14:34:58.783: W/System.err(1396):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
     08-23 14:34:58.783: W/System.err(1396):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
     08-23 14:34:58.783: W/System.err(1396):    at dalvik.system.NativeStart.main(Native Method)
     08-23 14:35:20.533: W/IInputConnectionWrapper(1396): showStatusIcon on inactive InputConnection

1 个答案:

答案 0 :(得分:1)

  db = DBHelper.getWritableDatabase();

尝试将此更改为

     db = this.getWritableDatabase();

因为你的助手类名不是db helper而你刚刚声明了DBHelper,这就是为什么它没有显示编译错误