0

我在验证我的 Android 应用程序登录时遇到问题。我有 2 个字段需要用户输入电子邮件和密码,如果两者都存在于数据库中,那么如果出现不正确的错误,它们将被带到主屏幕(登录成功)。我已经尝试了一切,但仍然无法正常工作!请帮助我在下面发布了我的代码。

package com.example.finalproject;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener{

    EditText mEmailAdd;
    EditText mPassword;
    private SQLiteAdapter mydb = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        //addListenerOnButton();
    }

    public void onCreateMainscreen(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screenmain_activity);

         Button mNewUser = (Button)findViewById(R.id.btnLogMain);
            mNewUser.setOnClickListener(this);
    }








    public void onClick(View v) {

        switch(v.getId()){

        case R.id.btnLogMain:
            mEmailAdd = (EditText)findViewById(R.id.email);
            mPassword = (EditText)findViewById(R.id.password);

            String uname = mEmailAdd.getText().toString();
            String pass = mPassword.getText().toString();

            if(uname.equals("") || uname == null){
                Toast.makeText(getApplicationContext(), "email Empty", Toast.LENGTH_SHORT).show();
            }else if(pass.equals("") || pass == null){
                Toast.makeText(getApplicationContext(), "Password Empty", Toast.LENGTH_SHORT).show();
            }else{
                boolean validLogin = validateLogin(uname, pass, LoginActivity.this);
                if(validLogin){
                    System.out.println("In Valid");
                    Intent i = new Intent(LoginActivity.this, MainMenuActivity.class);
                    startActivity(i);
                    finish();
                }
            }
            break;


        }
    }

        //  @SuppressWarnings("deprecation")

    public boolean validateLogin(String uemail, String pass, Context context) {

        mydb = new SQLiteAdapter(this);
        SQLiteAdapter db = mydb.openToWrite();
        //SELECT
        String[] columns = {"_id"};

        //WHERE clause
        String selection = "email=? AND password=?";

        //WHERE clause arguments
        String[] selectionArgs = {uemail,pass};

        Cursor cursor = null;
        try{
        //SELECT _id FROM login WHERE email=uemail AND password=pass
        cursor = db.query(SQLiteAdapter.MYDATABASE_TABLE, columns, selection, selectionArgs, null, null, null);

        //  startManagingCursor(cursor);

        }catch(Exception e){
            e.printStackTrace();
        }
        int numberOfRows = cursor.getCount();

        if(numberOfRows <= 0){

            Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
            return false;
        }


        return true;
    }

    public void onDestroy(){
        super.onDestroy();
        mydb.close();
    }


}

数据库类

package com.example.finalproject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter {

 public static final String MYDATABASE_NAME = "MY_PROJECT_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_USERS_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String KEY_NAME = "name";
 public static final String KEY_EMAIL = "email";
 public static final String KEY_PASSWORD = "password";

 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + MYDATABASE_TABLE + " ("
  + KEY_ID + " integer primary key autoincrement, "
  + KEY_NAME + " text not null, "
  + KEY_EMAIL + " text not null, "
  + KEY_PASSWORD + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 public SQLiteAdapter(Context c){
  context = c;
 }

 public SQLiteAdapter openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this; 
 }

 public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this; 
 }

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

 public long insert(String name, String email, String password){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_NAME, name);
  contentValues.put(KEY_EMAIL, email);
  contentValues.put(KEY_PASSWORD, password);
  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

 public int deleteAll(){
  return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
 }

 public Cursor queueAll(){
  String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_EMAIL,KEY_PASSWORD};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
    null, null, null, null, null);

  return cursor;
 }

 public class SQLiteHelper extends SQLiteOpenHelper {

  public SQLiteHelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(SCRIPT_CREATE_DATABASE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
  }
 } 
}
4

2 回答 2

0

不是你的错误,而是改变

if(uname.equals("") || uname == null){ // throws nullpointerexception if uname == null

if(uname == null || uname.length() == 0 ){ // throws no exception and also checks the " "

于 2013-02-09T21:30:31.933 回答
0

不确定这是否只是复制粘贴错误,但提供的代码不仅无法编译,而且也从未为登录按钮设置点击侦听器。这是我修改的内容,使其既可以编译又可以查询数据库。

SQLiteAdapter 中

public SQLiteDatabase openToWrite() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
            MYDATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    return sqLiteDatabase;
}

LoginActivity

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_activity);
    //addListenerOnButton();

    Button mNewUser = (Button)findViewById(R.id.btnLogMain);
    mNewUser.setOnClickListener(this);
}

public void onCreateMainscreen(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screenmain_activity);

     Button mNewUser = (Button)findViewById(R.id.btnLogMain);
     mNewUser.setOnClickListener(this);
}
 public boolean validateLogin(String uemail, String pass, Context context) {

    mydb = new SQLiteAdapter(this);
    SQLiteDatabase db = mydb.openToWrite();
    //SELECT
    String[] columns = {"_id"};

    //WHERE clause
    String selection = "email=? AND password=?";

    //WHERE clause arguments
    String[] selectionArgs = {uemail,pass};

    Cursor cursor = null;
    try{
    //SELECT _id FROM login WHERE email=uemail AND password=pass
    cursor = db.query(SQLiteAdapter.MYDATABASE_TABLE, columns, selection, selectionArgs, null, null, null);

    //  startManagingCursor(cursor);

    }catch(Exception e){
        e.printStackTrace();
    }
    int numberOfRows = cursor.getCount();

    if(numberOfRows <= 0){

        Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
        return false;
    }


    return true;
}

另请注意,此代码也永远不会将任何内容插入数据库。我认为这将在其他地方完成。此外,这里打破了许多命名约定和一般的良好实践。

几个问题:

  • 永远不要在主线程上做数据库工作。
  • 以“m”为前缀的变量表示它们是类的成员变量。
  • 确保在适当的时候使用@Override 表示法。
于 2013-02-09T23:31:19.650 回答