如何修复登录验证Android中的错误

时间:2014-08-05 05:23:28

标签: java android

尝试在我的Android屏幕上登录时遇到问题。我有两个字段要求用户输入用户名和密码,然后它应该去检查sqlite db如果正确它们将导航到其他屏幕(用户登录)如果不正确他们将通过Toast消息通知。我的问题是当我点击登录按钮我的应用程序显示"应用程序数据库意外停止。请再试一次"!请有人帮忙!!我的代码发布在下面

UserLogin.java:

public class UserLogin extends Activity {

public static String getName;
public static String getPassword;
EditText name,pwd;
Button login,signup;
DataBaseHandler dataBaseHandler;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ulogin);
    name = (EditText) findViewById(R.id.name);
    pwd = (EditText) findViewById(R.id.pwd);

    login = (Button) findViewById(R.id.login);
    signup = (Button) findViewById(R.id.signup);


    login.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            dataBaseHandler = new DataBaseHandler(getBaseContext());
            dataBaseHandler.open();
            String getName = name.getText().toString();
            String getPassword = pwd.getText().toString();

            System.out.println("print something");

            if(getName.equals("")||getName==null)
            {
                Toast.makeText(getBaseContext(), "User Name Empty", Toast.LENGTH_SHORT).show();
                System.out.println("User name empty");
            }
            else if(getPassword.equals("")||getPassword==null)
            {
                Toast.makeText(getBaseContext(), "Password Empty", Toast.LENGTH_SHORT).show();
                System.out.println("Password empty");
            }

            else
            {
                try
                {

                    Cursor c = db.rawQuery("SELECT txtUserName,txtPassword FROM mytable WHERE txtUserName= ? AND txtPassword= ?", new String[] {getName, getPassword});
                    if(c.moveToFirst())
                    {

                        Toast.makeText(getBaseContext(), "User Name and Password is Correct", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(UserLogin.this, LoggedIn.class);
                        startActivity(i);
                        System.out.println("User name and Password correct correct");
                    }
                    else
                    {
                        Toast.makeText(getBaseContext(), "User Name and Password is not Correct", Toast.LENGTH_SHORT).show();
                        System.out.println("User name and password is not correct");
                    }
                }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }

            }

            dataBaseHandler.close();

        }

    });

DataBaseHandler.java:

公共类DataBaseHandler {

public static final String NAME = "txtUserName";
public static final String PASSWORD = "txtPassword";
public static final String EMAIL = "email";
public static final String TABLE_NAME = "mytable";
public static final String DATA_BASE_NAME = "mydatabase";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_CREATE = "create table mytable(txtUserName text primary key not null, txtPassword password not null, email text not null);";

private DataBaseHelper dbHelper;
Context ctx;
SQLiteDatabase db;
public DataBaseHandler(Context ctx){
    this.ctx = ctx;
    dbHelper = new DataBaseHelper(ctx);
}
private static class DataBaseHelper extends SQLiteOpenHelper
{
    public DataBaseHelper(Context ctx){
        super(ctx,DATA_BASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try
        {
            db.execSQL(TABLE_CREATE);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS mytable");
        onCreate(db);

    }

}
public DataBaseHandler open(){
    db = dbHelper.getWritableDatabase();
    return this;
}

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

public long insertData(String txtUserName, String txtPassword, String email){
    ContentValues content = new ContentValues();
    content.put(NAME,txtUserName);
    content.put(PASSWORD,txtPassword);
    content.put(EMAIL,email);
    return db.insertOrThrow(TABLE_NAME, null, content);
}

public String getAllData()
{
    db = dbHelper.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, new String[] {NAME, PASSWORD, EMAIL}, null, null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while(cursor.moveToNext())
    {
        String name = cursor.getString(0);
        String pwd = cursor.getString(1);
        String email = cursor.getString(2);
        buffer.append(name+" "+pwd+" "+email+""+"\n");
    }
    return buffer.toString();
}



public Cursor returnData(){
    return db.query(TABLE_NAME, new String[] {NAME,PASSWORD,EMAIL}, null, null, null, null, null);
}

}

logcat的:

08-05 10:40:43.165: E/AndroidRuntime(373): FATAL EXCEPTION: main
08-05 10:40:43.165: E/AndroidRuntime(373): java.lang.NullPointerException
08-05 10:40:43.165: E/AndroidRuntime(373):  at com.example.databasesplash.UserLogin$1.onClick(UserLogin.java:67)
08-05 10:40:43.165: E/AndroidRuntime(373):  at android.view.View.performClick(View.java:2408)
08-05 10:40:43.165: E/AndroidRuntime(373):  at android.view.View$PerformClick.run(View.java:8816)
08-05 10:40:43.165: E/AndroidRuntime(373):  at android.os.Handler.handleCallback(Handler.java:587)
08-05 10:40:43.165: E/AndroidRuntime(373):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 10:40:43.165: E/AndroidRuntime(373):  at android.os.Looper.loop(Looper.java:123)
08-05 10:40:43.165: E/AndroidRuntime(373):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-05 10:40:43.165: E/AndroidRuntime(373):  at java.lang.reflect.Method.invokeNative(Native Method)
08-05 10:40:43.165: E/AndroidRuntime(373):  at java.lang.reflect.Method.invoke(Method.java:521)
08-05 10:40:43.165: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-05 10:40:43.165: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-05 10:40:43.165: E/AndroidRuntime(373):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

如果您查看代码,请使用db variable这样的

 Cursor c = db.rawQuery("SELECT txtUserName,txtPassword FROM mytable WHERE txtUserName= ? AND txtPassword= ?", new String[] {getName, getPassword});

但你声明它就像

SQLiteDatabase db;

所以db当前值为null,导致应用程序中的NPE

在你的dbhelper class

public DataBaseHandler open(){
    db = dbHelper.getWritableDatabase();
    return this;
}

我认为应该是

public SQLiteDatabase open(){
        db = dbHelper.getWritableDatabase();
        return db;
    }

但在你UserLogin Activity的{​​{1}}中,你可以像这样打电话给

onClick method

而不是

dataBaseHandler.open();