我已经使用登录功能进行了注册演示。当我使用凭据登录时,它会给我IndexOutOfBoundException。以下是我要求的代码&错误日志。
Login.java
package com.example.registrationexample;
import java.util.ArrayList;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends ActionBarActivity {
EditText edt_txt_username, edt_txt_pwdActual;
Button btn_LoginActual;
ArrayList<RegUserGetSet> singleUserData;
SharedPreferences sp;
public static String sharedPref = "sharedPrefUserData";
public static String UserFullName = "FullNameKey";
public static String UserEmail = "EmailKey";
public static String UserPwd = "PwdKey";
public static String UserDOB = "DOBKey";
public static String UserGender = "GenderKey";
DatabaseHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
edt_txt_username = (EditText) findViewById(R.id.edt_txt_LoginEmail);
edt_txt_pwdActual = (EditText) findViewById(R.id.edt_txt_LoginPwd);
btn_LoginActual = (Button) findViewById(R.id.btn_loginActual);
btn_LoginActual.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String username = edt_txt_username.getText().toString();
String pwd = edt_txt_pwdActual.getText().toString();
dbHandler = new DatabaseHandler(getBaseContext());
singleUserData = dbHandler.getUser(username, pwd);
if (singleUserData.size() == 1) {
// sharedPref(ArrayList < Login > singleUserData);
sp = getSharedPreferences(sharedPref, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(UserFullName, singleUserData.get(0)
.toString());
editor.putString(UserEmail, singleUserData.get(1)
.toString());
editor.putString(UserEmail, singleUserData.get(2)
.toString());
editor.putString(UserEmail, singleUserData.get(3)
.toString());
editor.putString(UserEmail, singleUserData.get(4)
.toString());
editor.commit();
Intent i_welcome = new Intent(getApplicationContext(),
Welcome.class);
startActivity(i_welcome);
Toast.makeText(getApplicationContext(),
"You are successfully login...!!",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Credentials not matched...!!", Toast.LENGTH_LONG)
.show();
}
}
private void sharedPref(ArrayList<Login> singleUserSharedPrefData) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
DatabaseHandler.java
package com.example.registrationexample;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
static int DB_VERSION = 1;
static String DB_NAME = "users_info";
String TABLE_NAME = "users";
String KEY_ID = "user_id";
String KEY_FULL_NAME = "user_full_name";
String KEY_EMAIL = "user_email";
String KEY_PWD = "user_pwd";
String KEY_DOB = "user_dob";
String KEY_GENDER = "user_gender";
public DatabaseHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID
+ " INTEGER PRIMARY KEY, " + KEY_FULL_NAME + " TEXT, "
+ KEY_EMAIL + " TEXT, " + KEY_PWD + " TEXT, " + KEY_DOB
+ " TEXT, " + KEY_GENDER + " TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long registerUser(RegUserGetSet regUserGetSet) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FULL_NAME, regUserGetSet.getFname() + " "
+ regUserGetSet.getLname());
values.put(KEY_EMAIL, regUserGetSet.getEmail());
values.put(KEY_PWD, regUserGetSet.getPwd());
values.put(KEY_DOB, regUserGetSet.getDob());
values.put(KEY_GENDER, regUserGetSet.getGender());
long result = db.insert(TABLE_NAME, null, values);
Log.e("Inserted Data", "" + result);
Log.e("Fname & Lname", "" + regUserGetSet.getFname() + " "
+ regUserGetSet.getLname());
db.close();
return result;
}
public ArrayList<RegUserGetSet> getAllUser() {
// TODO Auto-generated method stub
ArrayList<RegUserGetSet> allUsers = new ArrayList<RegUserGetSet>();
String select_query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(select_query, null);
if (cursor.moveToFirst()) {
do {
RegUserGetSet singleUserObj = new RegUserGetSet();
singleUserObj.setFname(cursor.getString(cursor
.getColumnIndex(KEY_FULL_NAME)));
singleUserObj.setLname(cursor.getString(cursor
.getColumnIndex(KEY_FULL_NAME)));
singleUserObj.setEmail(cursor.getString(cursor
.getColumnIndex(KEY_EMAIL)));
singleUserObj.setPwd(cursor.getString(cursor
.getColumnIndex(KEY_PWD)));
singleUserObj.setDob(cursor.getString(cursor
.getColumnIndex(KEY_DOB)));
singleUserObj.setGender(cursor.getString(cursor
.getColumnIndex(KEY_GENDER)));
allUsers.add(singleUserObj);
} while (cursor.moveToNext());
}
db.close();
cursor.close();
return allUsers;
}
public ArrayList<RegUserGetSet> getUser(String username, String pwd) {
// TODO Auto-generated method stub
ArrayList<RegUserGetSet> singleUser = new ArrayList<RegUserGetSet>();
String select_user = "SELECT * FROM " + TABLE_NAME + " WHERE "
+ KEY_EMAIL + "='" + username + "' AND " + KEY_PWD + "='" + pwd
+ "'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(select_user, null);
if (cursor.moveToFirst()) {
do {
RegUserGetSet obj = new RegUserGetSet();
// obj.setUser_id(cursor.getString(cursor.getColumnIndex(KEY_ID)));
obj.setFname(cursor.getString(cursor
.getColumnIndex(KEY_FULL_NAME)));
obj.setLname(cursor.getString(cursor
.getColumnIndex(KEY_FULL_NAME)));
obj.setEmail(cursor.getString(cursor.getColumnIndex(KEY_EMAIL)));
obj.setPwd(cursor.getString(cursor.getColumnIndex(KEY_PWD)));
obj.setDob(cursor.getString(cursor.getColumnIndex(KEY_DOB)));
obj.setGender(cursor.getString(cursor
.getColumnIndex(KEY_GENDER)));
singleUser.add(obj);
} while (cursor.moveToNext());
}
db.close();
cursor.close();
return singleUser;
}
}
错误记录
07-02 13:34:22.924: E/AndroidRuntime(631): FATAL EXCEPTION: main
07-02 13:34:22.924: E/AndroidRuntime(631): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
07-02 13:34:22.924: E/AndroidRuntime(631): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
07-02 13:34:22.924: E/AndroidRuntime(631): at java.util.ArrayList.get(ArrayList.java:311)
07-02 13:34:22.924: E/AndroidRuntime(631): at com.example.registrationexample.Login$1.onClick(Login.java:63)
07-02 13:34:22.924: E/AndroidRuntime(631): at android.view.View.performClick(View.java:2408)
07-02 13:34:22.924: E/AndroidRuntime(631): at android.view.View$PerformClick.run(View.java:8816)
07-02 13:34:22.924: E/AndroidRuntime(631): at android.os.Handler.handleCallback(Handler.java:587)
07-02 13:34:22.924: E/AndroidRuntime(631): at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 13:34:22.924: E/AndroidRuntime(631): at android.os.Looper.loop(Looper.java:123)
07-02 13:34:22.924: E/AndroidRuntime(631): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-02 13:34:22.924: E/AndroidRuntime(631): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 13:34:22.924: E/AndroidRuntime(631): at java.lang.reflect.Method.invoke(Method.java:521)
07-02 13:34:22.924: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-02 13:34:22.924: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-02 13:34:22.924: E/AndroidRuntime(631): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在这里:if (singleUserData.size() == 1)
你好像在检查大小是1个元素。但是在同一范围内,您正在访问多个项目。
您可能需要更改此检查:if (singleUserData.size() == 1)
至if (singleUserData.size() == 5)
。