我在數據庫文件中出現以上錯誤。我是編碼新手。請幫我解決這個問題。我不瞭解這些類型的問題。請給我建議。錯誤 - 無法啓動活動ComponentInfo:android.database.CursorIndexOutOfBoundsException:索引0請求,大小爲0
我的數據庫代碼如下所示:
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.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "applicationDB";
// Employee table name
private static final String TABLE_USERS = "apps";
private static String SlNo = "slno";
private static String NAME = "name";
private static String PHONE = "phone";
private static String EMAIL = "email";
private static String ADDRESS = "address";
private static String COMMENTS = "comments";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USER_TABLE = " CREATE TABLE " + TABLE_USERS + "("
+ SlNo + " INTEGER PRIMARY KEY, "
+ NAME + " TEXT, "
+ PHONE + " TEXT, "
+ EMAIL + " TEXT, "
+ ADDRESS + " TEXT, "
+ COMMENTS + " TEXT "
+ ")";
db.execSQL(CREATE_USER_TABLE);
//db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
Log.e(NAME, "Error is here");
Log.e(PHONE, "Error is here");
Log.e(EMAIL, "Error is here");
Log.e(ADDRESS, "Error is here");
Log.e(COMMENTS, "Error is here");
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
}
// Adding new user
public int addUser(User user)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, user.getName());
values.put(ADDRESS, user.getAddress());
values.put(PHONE, user.getPhone());
values.put(EMAIL, user.getEmail());
values.put(COMMENTS, user.getComments());
// Inserting Row
int id = (int) db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
return id;
}
//updating a existing user
public int editUser(User user, String position) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, user.getName());
values.put(ADDRESS, user.getAddress());
values.put(PHONE, user.getPhone());
values.put(EMAIL, user.getEmail());
values.put(COMMENTS, user.getComments());
// updating row
return db.update(TABLE_USERS, values, SlNo + " = ?",
new String[] { String.valueOf(position) });
}
public User getUser(String id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS, new String[] {
SlNo, NAME, ADDRESS, PHONE, EMAIL, COMMENTS }, SlNo + "=?",
new String[] {String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
User user = new User(cursor.getString(1),
cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
db.close();
// return user
return user;
}
// LIst all users
public String[] getAllUsers() {
ArrayList<String> usersList = new ArrayList<String>();
Log.e(NAME, "Error is here");
Log.e(PHONE, "Error is here");
Log.e(EMAIL, "Error is here");
Log.e(ADDRESS, "Error is here");
Log.e(COMMENTS, "Error is here");
String selectQuery = " SELECT * FROM " + TABLE_USERS;
try {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
String user = cursor.getString(1) + ", " + cursor.getString(2) + ", " + cursor.getString(3) + ", " + cursor.getString(4) + ", " + cursor.getString(5);
usersList.add(user);
} while (cursor.moveToNext());
}
String[] users = new String[usersList.size()];
db.close();
return (usersList.toArray(users));
}
catch(Exception e) {
Log.d("Error in getting users from DB", e.getMessage());
return null;
}
}
}
由我所觀察到的logcat的痕跡,如下圖所示:
12-23 04:12:29.037: D/value of position from intent(3072): null
12-23 04:12:29.407: D/AndroidRuntime(3072): Shutting down VM
12-23 04:12:29.427: W/dalvikvm(3072): threadid=1: thread exiting with uncaught exception (group=0xb0f5e648)
12-23 04:12:29.487: E/AndroidRuntime(3072): FATAL EXCEPTION: main
12-23 04:12:29.487: E/AndroidRuntime(3072): java.lang.RuntimeException: Unable to start activity ComponentInfo{form.app.applicationform1/form.app.applicationform1.DetailsUsers}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.os.Looper.loop(Looper.java:137)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-23 04:12:29.487: E/AndroidRuntime(3072): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 04:12:29.487: E/AndroidRuntime(3072): at java.lang.reflect.Method.invoke(Method.java:525)
12-23 04:12:29.487: E/AndroidRuntime(3072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-23 04:12:29.487: E/AndroidRuntime(3072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-23 04:12:29.487: E/AndroidRuntime(3072): at dalvik.system.NativeStart.main(Native Method)
12-23 04:12:29.487: E/AndroidRuntime(3072): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
12-23 04:12:29.487: E/AndroidRuntime(3072): at form.app.applicationform1.DatabaseHandler.getUser(DatabaseHandler.java:97)
12-23 04:12:29.487: E/AndroidRuntime(3072): at form.app.applicationform1.DetailsUsers.onCreate(DetailsUsers.java:42)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.Activity.performCreate(Activity.java:5133)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-23 04:12:29.487: E/AndroidRuntime(3072): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-23 04:12:29.487: E/AndroidRuntime(3072): ... 11 more
12-23 04:14:59.689: I/Process(3072): Sending signal. PID: 3072 SIG: 9
請幫助我。
非常感謝。這工作,錯誤消息沒有再次出現。再次感謝。 –