데이터를 검색하는 데 문제 make sure cursor is initialized correctly before accessing data from it
가 있습니다. 커서가 제대로 초기화 된 문제를 파악할 수 없다고합니다.
아래 내 logcat.
02-16 20:34:49.678 22333-22333/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.computer.mathkiddofinal:second, PID: 22333
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.computer.mathkiddofinal/com.example.computer.mathkiddofinal.Database.Record_DB}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetLong(Native Method)
at android.database.CursorWindow.getLong(CursorWindow.java:507)
at android.database.CursorWindow.getInt(CursorWindow.java:574)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:80)
at com.example.computer.mathkiddofinal.Database.Record_DB.onCreate(Record_DB.java:55)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
Record_DB.java,이 줄의 오류 int postwh = res.getInt(res.getColumnIndex("POSTWH"));
public class Record_DB extends Activity {
Final_Result_Grade_4 asd;
TextView txtDB;
DatabaseHelper myDb;
ArrayList<String> results = new ArrayList<String>();
ArrayList<Integer> lpostwh = new ArrayList<Integer>();
ArrayList<Integer> lpostmul = new ArrayList<Integer>();
ArrayList<Integer> lpostdiv = new ArrayList<Integer>();
ArrayList<Integer> lprewh = new ArrayList<Integer>();
ArrayList<Integer> lpremul = new ArrayList<Integer>();
ArrayList<Integer> lprediv = new ArrayList<Integer>();
ListView errorListview;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record__db);
errorListview = (ListView) findViewById(R.id.listtt);
//TextView txtDB = (TextView) findViewById(R.id.txtDB);
// txtDB.setMovementMethod(new ScrollingMovementMethod());
myDb = new DatabaseHelper(this);
Cursor res = myDb.getAllData();
int cols = res.getColumnCount();
if (res.getCount() == 0) {
Toast.makeText(Record_DB.this, "no data", Toast.LENGTH_SHORT).show();
return;
}
while (res.moveToNext()) {
int id = res.getInt(res.getColumnIndex("ID"));
String name = res.getString(res.getColumnIndex("NAME"));
int pre = res.getInt(res.getColumnIndex("PRETEST"));
int post = res.getInt(res.getColumnIndex("POSTTEST"));
int postwh = res.getInt(res.getColumnIndex("POSTWH"));
int postmul = res.getInt(res.getColumnIndex("POSTMUL"));
int postdiv = res.getInt(res.getColumnIndex("POSTDIV"));
int prewh = res.getInt(res.getColumnIndex("PREWH"));
int premul = res.getInt(res.getColumnIndex("PREMUL"));
int prediv = res.getInt(res.getColumnIndex("PREDIV"));
results.add(""+postwh + postmul + postdiv + prewh + premul + prediv );
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results);
errorListview.setAdapter(adapter);
이 문제를 해결하는 방법을 알 수 없으며 누구나 도울 수 있습니다. 정말 감사합니다
DataBaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "gradefour.db";
public static final String TABLE_NAME = "grade_four_table";
public static final String COL_1= "ID";
public static final String COL_2= "NAME";
public static final String COL_3= "PRETEST";
public static final String COL_4= "POSTTEST";
public static final String COL_5= "POSTWH";
public static final String COL_6= "POSTMUL";
public static final String COL_7= "POSTDIV";
public static final String COL_8= "PREWH";
public static final String COL_9= "PREMUL";
public static final String COL_10= "PREDIV";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRETEST INTEGER,POSTTEST INTEGER,POSTWH INTEGER,POSTMUL INTEGER,POSTDIV INTEGER,PREWH INTEGER,PREMUL INTEGER,PREDIV INTEGER) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public boolean inserData(String name, Integer pretest, Integer posttest, Integer postwh, Integer postmul, Integer postdiv,
Integer prewh, Integer premul, Integer prediv){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,pretest);
contentValues.put(COL_4,posttest);
contentValues.put(COL_5,postwh);
contentValues.put(COL_6,postmul);
contentValues.put(COL_7,postdiv);
contentValues.put(COL_8,prewh);
contentValues.put(COL_9,premul);
contentValues.put(COL_10,prediv);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result==-1){
return false;
}
else
{
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
}
귀하의 의견에 따라 .. !!
앱을 설치 한 후 6 개의 열 을 추가했습니다 . 따라서 새 테이블이 생성 되고 테이블에 6 개의 열이 추가되면 앱을 다시 설치 해야합니다.
OnCreate () 메서드는 응용 프로그램이 설치되고 테이블이 생성 될 때만 호출되므로 몇 가지 변경이 필요한 경우 두 가지 옵션이 있습니다.
OnUpgrade () 메서드를 변경하고 앱을 업데이트합니다. (라이브 애플리케이션을 위해하십시오)
앱을 제거하고 다시 설치하십시오. (테스트 용)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다