用SQLite实现登录功能,点登录就会崩溃,求解答啊

用SQLite实现登录功能,点登录就会崩溃,求解答啊

这个是LoginActivity

 package com.sylvanas.qq;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private CheckBox remenberPass;
    private TextView register;
    private MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        //登录
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        remenberPass = (CheckBox) findViewById(R.id.remember_pass);
        login = (Button) findViewById(R.id.button);
        boolean isRemember = pref.getBoolean("remember_password",false);
        if (isRemember){
            String account = pref.getString("account","");
            String password = pref.getString("password","");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            remenberPass.setChecked(true);
        }
        login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                String password_in_SQL = "";
                //验证账号密码
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Login",null,"name = ?",new String[]{account},null,null,null);
                if(cursor.moveToNext()){
                    do{
                        password_in_SQL = cursor.getString(cursor.getColumnIndex("password"));
                        Log.d("LoginActivity","password:"+password_in_SQL);
                    }while (cursor.moveToNext());
                }
                cursor.close();
                if(password.equals(password_in_SQL)) {
                    editor = pref.edit();
                    if (remenberPass.isChecked()) {
                        editor.putBoolean("remember_password",true);
                        editor.putString("account",account);
                        editor.putString("password",password);
                    }else {
                        editor.clear();
                    }
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else if (account.equals("")){
                    Toast.makeText(LoginActivity.this,"请输入用户名",
                            Toast.LENGTH_SHORT).show();
                }
                else if (password.equals("")){
                    Toast.makeText(LoginActivity.this,"请输入密码",
                            Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        //注册
        register = (TextView) findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}

刚开始建数据库和插入了一组账号密码

 package com.sylvanas.qq;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;

public class FirstActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        dbHelper = new MyDatabaseHelper(this,"User.db",null,1);
        dbHelper.getWritableDatabase();

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("account","MZ");
        values.put("password","MZ");
        db.insert("Login",null,values);
        db.close();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(FirstActivity.this,LoginActivity.class);
                startActivity(intent);
                FirstActivity.this.finish();
            }
        }, 2000);
    }
}

实在是不会了,求大神解答

1个回答

我刚刚也在做这个 你是不是 在第一个页面 少输入了 dbHelper = new MyDatabaseHelper(this,"User.db",null,1);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问