从Sqlite数据库行中获取具有特定值的信息时失败

时间:2019-04-20 17:05:42

标签: java android android-sqlite

因此,我想使用配置文件创建登录会话。当用户打开活动时,我想获取个人资料信息,例如全名,简称,出生,性别,电子邮件和密码。我做了getUserInfo(String email)来在Sqlite表行中搜索像email这样的值,这样我就可以获取其他表信息。

这是我的getuserinfo():

public void getUserInfo(String email) {

        SQLiteDatabase db = getReadableDatabase();
        String[] columns = { KEY_ROWID, KEY_FULLNAME, KEY_SHORTNAME, KEY_BIRTH, KEY_GENDER, KEY_EMAIL, KEY_PASSWORD };
        String[] selectionArgs = { String.valueOf(email) };

        Cursor cursor = db.query(TABLE_NAME, columns, null, selectionArgs, null, null, null);
        if (null != cursor) {

            cursor.moveToFirst();
            profile.setmId(cursor.getInt(0));
            profile.setmFullName(cursor.getString(1));
            profile.setmShortName(cursor.getString(2));
            profile.setmBirth(cursor.getString(3));
            profile.setmGender(cursor.getString(4));
            profile.setmEmail(cursor.getString(5));
            profile.setmPassword(cursor.getString(6));
        }
        db.close();
    }

我的MainActivity:

public class HomePage extends AppCompatActivity {

    SessionHandler sessionHandler;
    DatabaseHandler databaseHandler;
    String fullname, shortname, birth, gender, email, password;
    TextView tvFullname, tvShortname, tvBirth, tvGender, tvEmail, tvPassword;
    Profile profile;
    HashMap<String, String> a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        tvFullname = findViewById(R.id.tvFullname);
        tvShortname = findViewById(R.id.tvShortname);
        tvBirth = findViewById(R.id.tvBirth);
        tvGender = findViewById(R.id.tvGender);
        tvEmail = findViewById(R.id.tvEmail);
        tvPassword = findViewById(R.id.tvPassword);

        sessionHandler = new SessionHandler(this);
        sessionHandler.checkLogin();
        a = sessionHandler.getUserDetails();
        email = a.get(KEY_EMAIL);
        databaseHandler.getUserInfo(email);

        fullname = profile.getmFullName();
        shortname = profile.getmShortName();
        birth = profile.getmBirth();
        gender = profile.getmGender();
        password = profile.getmPassword();

        tvFullname.setText(fullname);
        tvShortname.setText(shortname);
        tvBirth.setText(birth);
        tvGender.setText(gender);
        tvPassword.setText(password);
    }

    public void LogoutBtn(View view) {

        sessionHandler.logoutUser();
    }
}

我的SessionHandler,以防万一也是错误的:

public HashMap<String, String> getUserDetails(){

        HashMap<String, String> user = new HashMap<String, String>();
        user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
        user.put(KEY_PASS, pref.getString(KEY_PASS, null));
        return user;
    }

这是我创建登录会话的时间:

public void createLoginSession(String email, String password) {

        editor.putBoolean(IS_LOGIN, true);
        editor.putString(KEY_EMAIL, email);
        editor.putString(KEY_PASS, password);
        editor.commit();
        mEmail = email;
    }

日志:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.accountdatabase.DatabaseHandler.getUserInfo(java.lang.String)' on a null object reference

对此有什么解决方案吗?我是android开发的新手,所以谢谢:D

对不起,我的语言不好

0 个答案:

没有答案