我刚刚开始使用android编码。我只下载了这段代码并进行了一些更改。当我尝试运行应用程序并输入错误的凭据时,它工作正常。但是当我输入正确的凭据时,应用程序将自动关闭。我尽力修复错误,但我失败了。希望你能帮助我们并指导我如何解决这个错误。
Login.java
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Now store the user in SQLite
//String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String br_code = user.getString("br_code");
String mem_id = user.getString("mem_id");
String username = user.getString("username");
String email = user.getString("email");
String created_at = user.getString("created_at");
// Inserting row in users table
db.addUser(br_code, mem_id, username, email, created_at);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
SqlHandler.java
private static final String BR_CODE = "br_code";
private static final String MEM_ID = "mem_id";
private static final String MEM_USERNAME = "username";
private static final String MEM_EMAIL = "email";
private static final String MEM_CREATED_AT = "created_at";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_MEMBERS + "("
+ BR_CODE + " INTEGER,"
+ MEM_ID + " INTEGER PRIMARY KEY,"
+ MEM_USERNAME + " TEXT,"
+ MEM_EMAIL + " TEXT UNIQUE,"
+ MEM_CREATED_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBERS);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String br_code, String mem_id, String username, String email, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BR_CODE, br_code); // branch code
values.put(MEM_ID, mem_id); // mem id
values.put(MEM_USERNAME, username); // username
values.put(MEM_EMAIL, email); // Email
values.put(MEM_CREATED_AT, created_at); // Created At
// Inserting Row
long id = db.insert(TABLE_MEMBERS, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_MEMBERS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("br_code", cursor.getString(1));
user.put("mem_id", cursor.getString(2));
user.put("username", cursor.getString(3));
user.put("email", cursor.getString(4));
user.put("created_at", cursor.getString(5));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
错误日志
05-19 16:23:24.730 1679-1679/info.androidhive.loginandregistration E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4746)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:434)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at info.androidhive.loginandregistration.helper.SQLiteHandler.getUserDetails(SQLiteHandler.java:102)
at info.androidhive.loginandregistration.activity.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:5080)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4746)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
at dalvik.system.NativeStart.main(Native Method)
05-19 16:23:27.420 1679-1679/info.androidhive.loginandregistration I/Process: Sending signal. PID: 1679 SIG: 9
答案 0 :(得分:0)
无法从CursorWindow读取第0行第5行。
您有5列,起始索引为0,但您从1开始
所以错误就在这里:
if (cursor.getCount() > 0) {
user.put("br_code", cursor.getString(1));
user.put("mem_id", cursor.getString(2));
user.put("username", cursor.getString(3));
user.put("email", cursor.getString(4));
user.put("created_at", cursor.getString(5));
}
只需从ids中减去1
if (cursor.getCount() > 0) {
user.put("br_code", cursor.getString(0));
user.put("mem_id", cursor.getString(1));
user.put("username", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
答案 1 :(得分:0)
你要将LoginActivity转到MainActivty。你在Manifest文件中定义了MainActivity吗?如果是,那么请检查下一步从数据库中获取数据的方法。
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contacts contact = new Contacts();
contact.set_id(cursor.getInt(0));
contact.set_addBookingId(cursor.getInt(1));
contact.set_bookingDate(cursor.getString(2));
contact.set_pubId(Integer.parseInt(cursor.getString(3)));
contact.set_pubdate(cursor.getString(4));
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}