1

尝试在我的 android 屏幕上登录时遇到了一个大问题。我有两个字段需要用户输入电子邮件和密码,然后它应该去检查 sqlite db,如果正确,他们将被导航到主屏幕(登录成功),如果不正确,他们将通过 toast 消息得到通知。我的问题是当我点击登录按钮时,我的应用程序显示“不幸停止工作”我在我的代码中看不到任何错误!请有人帮忙!!我的代码发布在 *LoginActivity下面

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 {


    Button mLogin;
    Button mNewUser;
    Button mShowAll;
    EditText mUsername;
    EditText mPassword;
    SQLiteAdapter mydb = null;
    private String uname;
    private String pass;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

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

        mLogin = (Button)findViewById(R.id.buttonLogin);
        mLogin.setOnClickListener(this);

        mShowAll = (Button)findViewById(R.id.buttonShowAll);
        mShowAll.setOnClickListener(this);
    }

    public void onClick(View v) {
        SQLiteDatabase db = new SQLiteAdapter(LoginActivity.this).openToWrite();
        if (v.getId() == R.id.buttonLogin) {
            mUsername = (EditText)findViewById(R.id.Username);
            mPassword = (EditText)findViewById(R.id.Password);

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

            if(uname.equals("") || uname == null){
                Toast.makeText(getApplicationContext(), "Username Empty", 
                        Toast.LENGTH_SHORT).show();
            }
            else if(pass.equals("") || pass == null){
            Toast.makeText(getApplicationContext(), "Password Empty", 
             Toast.LENGTH_SHORT).show();
            }
            }

        Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= ? AND password=?", new String[] {uname, pass});
        if(c.moveToFirst()) {
            Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
            Intent main = new Intent(LoginActivity.this, MainMenuActivity.class);
            startActivity(main);
        } 
        else {
            Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();

        }

    }
}

SQLite适配器

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;
import android.widget.Toast;

public class SQLiteAdapter {

 public static final String MYDATABASE_NAME = "MY_DEVELOPMENT_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";


 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(SQLiteAdapter sqLiteAdapter) {
    // TODO Auto-generated constructor stub
}

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

 public SQLiteDatabase openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return sqLiteDatabase;
    }
 /*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
  }
 }




}

注册活动

package com.example.finalproject;






import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
//import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {

 EditText inputName, inputEmail, inputPassword;
 Button buttonRegister, buttonDeleteAll;
 TextView login;
 private SQLiteAdapter mySQLiteAdapter;
 ListView listContent;

 SimpleCursorAdapter cursorAdapter;
 Cursor cursor;


   /** Called when the activity is first created. */

@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.register_activity);


       inputName = (EditText)findViewById(R.id.name);
       inputEmail = (EditText)findViewById(R.id.email);
       inputPassword = (EditText)findViewById(R.id.password);
       buttonRegister = (Button)findViewById(R.id.register);
       //buttonDeleteAll = (Button)findViewById(R.id.showAll);

       listContent = (ListView)findViewById(R.id.contentlist);

       mySQLiteAdapter = new SQLiteAdapter(this);
       mySQLiteAdapter.openToWrite();

       cursor = mySQLiteAdapter.queueAll();
      // String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_NAME, SQLiteAdapter.KEY_EMAIL,SQLiteAdapter.KEY_PASSWORD};
       /*int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3};
       cursorAdapter =
        new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
       listContent.setAdapter(cursorAdapter);*/

       buttonRegister.setOnClickListener(buttonAddOnClickListener);
       //buttonShowAll.setOnClickListener(buttonShowAllOnClickListener);
       //addListenerOnRegButton();

       TextView loginScreen = (TextView) findViewById(R.id.login);

       // Listening to login 
       loginScreen.setOnClickListener(TextLoginOnClickListener);
}

TextView.OnClickListener TextLoginOnClickListener
= new TextView.OnClickListener(){



@Override
public void onClick(View arg0) {
    Intent i = new Intent(getApplicationContext(), LoginActivity.class);
    startActivity(i);

}
};

   Button.OnClickListener buttonAddOnClickListener
   = new Button.OnClickListener(){



  @Override
  public void onClick(View arg0) {



   String data1 = inputName.getText().toString();
   String data2 = inputEmail.getText().toString();
   String data3 = inputPassword.getText().toString();

   boolean invalid = false;

    if(data1.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Name Missing", Toast.LENGTH_SHORT).show();
    }else if(data2.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
    }else if(data3.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
    }

    if(invalid == false){
        mySQLiteAdapter.insert(data1, data2, data3);
        updateList();
           Toast.makeText(RegisterActivity.this, "You are now registered",
                   Toast.LENGTH_SHORT).show();

        Intent i_register = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(i_register);

        inputName.setText("");
        inputEmail.setText("");
        inputPassword.setText("");
        finish();
    }   




  }

   };




  /*Button.OnClickListener buttonShowAllOnClickListener1
   = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
      Intent i = new Intent(RegisterActivity.this, MainMenuActivity.class);
        startActivity(i);
        finish();
  }

   };

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  mySQLiteAdapter.close();
 }*/



 private void updateList(){
  //cursor.requery();
   }

}

日志猫

02-04 02:55:56.085: E/AndroidRuntime(17402): FATAL EXCEPTION: main
02-04 02:55:56.085: E/AndroidRuntime(17402): java.lang.IllegalArgumentException: the bind value at index 2 is null
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at com.example.finalproject.LoginActivity.onClick(LoginActivity.java:62)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.view.View.performClick(View.java:4202)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.view.View$PerformClick.run(View.java:17340)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.os.Handler.handleCallback(Handler.java:725)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.os.Looper.loop(Looper.java:137)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at java.lang.reflect.Method.invoke(Method.java:511)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-04 02:55:56.085: E/AndroidRuntime(17402):    at dalvik.system.NativeStart.main(Native Method)
02-04 02:55:56.555: D/dalvikvm(17402): GC_CONCURRENT freed 261K, 11% free 3060K/3436K, paused 126ms+33ms, total 459ms
02-04 03:00:56.444: I/Process(17402): Sending signal. PID: 17402 SIG: 9
4

2 回答 2

1

您的问题出现在这一行:

db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= ? AND password=?", new String[] {uname, pass})

发生的情况是,即使您的if语句输出 Toast 表示编辑文本为空,此部分也会被执行,因此unamepass空,这会给您带来IllegalArgumentException: the bind value at index 2 is null错误,因为您正在传递 SQL 函数空值。

简单修复:仅在值不为空或不为空时才执行代码的底部:

public void onClick(View v) {
    SQLiteDatabase db = new SQLiteAdapter(LoginActivity.this).openToWrite();
    if (v.getId() == R.id.buttonLogin) {
        mUsername = (EditText)findViewById(R.id.Username);
        mPassword = (EditText)findViewById(R.id.Password);

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

        if(uname.equals("") || uname == null){
            Toast.makeText(getApplicationContext(), "Username Empty", 
            Toast.LENGTH_SHORT).show();
        }
        else if(pass.equals("") || pass == null){

            Toast.makeText(getApplicationContext(), "Password Empty", 
            Toast.LENGTH_SHORT).show();

        } else {

            Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= ? AND password=?", new String[] {uname, pass});
            if(c.moveToFirst()) {
                 Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                 Intent main = new Intent(LoginActivity.this, MainMenuActivity.class);
                 startActivity(main);
            }  else {
                 Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();

            }
       } 
    }
}
于 2013-02-15T12:05:05.773 回答
1

该线程可能会对您有所帮助... IllegalArgumentException:索引 1 处的绑定值为 null

检查您的 selArgs 是否不为空 ..

于 2013-02-15T11:59:03.640 回答