用户名/密码验证SQLite Android

时间:2015-08-18 06:30:28

标签: android sqlite

我正在尝试在Android中创建一个登录页面,但每次运行它时程序都会崩溃。 这是我的代码:

b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String a = ed1.getText().toString();
            String b = ed2.getText().toString();
            String repassword;
            if (a.length() <= 0 || b.length() <= 0) {
                if (a.length() <= 0) {
                    ed1.setError("Please enter your username.");
                }
                if (b.length() <= 0) {
                    ed2.setError("Please enter your password.");
                }
            } else {
                Cursor cursor = sq
                        .query("AccountRegistration", null, " Username=?",
                                new String[] { a }, null, null, null);

                if (cursor.getCount() < 1) {
                    cursor.close();
                    repassword = "NOT EXIST";
                }
                cursor.moveToFirst();

                repassword = cursor.getString(cursor
                        .getColumnIndex("Password"));
                cursor.close();

                String storedPassword = repassword;

                if (b.equals(storedPassword)) {
                    Toast.makeText(getApplicationContext(),
                            "You have been logged in.", Toast.LENGTH_LONG)
                            .show();
                    Intent ii = new Intent(MainActivity.this,
                            WelcomePage.class);
                    startActivity(ii);
                } else {
                    Toast.makeText(
                            getApplicationContext(),
                            "Please check your credentials or create a new account.",
                            Toast.LENGTH_LONG).show();
                    ed1.setText("");
                    ed2.setText("");
                }
            }
        }
    });

当我尝试调试时,我注意到错误发生在我将cursor.getString(cursor.getColumnIndex(&#34; Password&#34;))存储到repassword变量的部分。我的logcat说cursorindexoutofbounds。

0 个答案:

没有答案