01-05 21:04:16.138: E/CursorWindow(16448): Failed to read row 0, column -1 from a CursorWindow which has 16 rows, 4 columns.
请帮助,我把主要和数据库以及视图类放在
之下主要课程:
public class MainActivity extends Activity {
Button add, choose, display;
TextView contactsdisplay;
DBgenerator db;// Instantiate the database to be used by this method//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.addbutton);
choose = (Button) findViewById(R.id.choosebutton);
display = (Button) findViewById(R.id.displaybutton);
contactsdisplay = (TextView) findViewById(R.id.contactsdisplay);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent j = new Intent(MainActivity.this, Manipulator.class);
startActivity(j);
}
});
choose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent k = new Intent(MainActivity.this, DisplayEdit.class);
startActivity(k);
}
});
display.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent f = new Intent(MainActivity.this, DisplayContacts.class);
startActivity(f);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
数据库类:
public class DBgenerator {
public static final String Key_ROWID = "_id";// the row id//
public static final String Key_FirstName = "Person_firstname";
public static final String Key_LastName = "Person_lastname";
public static final String Key_Mobile = "Mobile";
public static final String Key_Address = "Address";
private static final String Database_name = "contacts";
private static final String Database_table = "contactstable";
private static final int Database_version = 1;
// create database//
public static final String DATABASE_CREATE = " CREATE TABLE "
+ Database_table + "(" + Key_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + Key_FirstName
+ " TEXT NOT NULL, " + Key_LastName + " TEXT NOT NULL, "
+ Key_Mobile + " TEXT NOT NULL, " + Key_Address
+ " TEXT NOT NULL);";
private dbmanipulation manipulate;// make an instance of the inner class to
// be frequently used//
private Context mycontext;
private SQLiteDatabase mydb;
private static class dbmanipulation extends SQLiteOpenHelper {
public dbmanipulation(Context context) {
super(context, Database_name, null, Database_version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// CREATE AND EXECUTE YOUR DATABASE
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + Database_table);
onCreate(db);
}
}
public DBgenerator(Context c) {
/*
* i am not sure why he did this one but i am sure that this is used to
* take in the context of which activity will be using the database
* which was probably one of the things i was missing in the SQLite
*/
mycontext = c;
}
public DBgenerator open() {
// TODO Auto-generated method stub
manipulate = new dbmanipulation(mycontext);
mydb = manipulate.getWritableDatabase();
return this;
}
public void close() {
// TODO Auto-generated method stub
manipulate.close();
}
public long addnewcontact(String firstName, String lastName,
String mobileno, String contactAddress) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(Key_FirstName, firstName);
cv.put(Key_LastName, lastName);
cv.put(Key_Mobile, mobileno);
cv.put(Key_Address, contactAddress);
return mydb.insert(Database_table, null, cv);
}
public String getallcontacts() {
// TODO Auto-generated method stub
String[] Columns = new String[] { Key_FirstName, Key_LastName,
Key_Mobile, Key_Address };
Cursor c = mydb.query(Database_table, Columns, null, null, null, null,
null);
// we need to select and search for the users by thier IDs//
int contactrow = c.getColumnIndex(Key_ROWID);
int contactfname = c.getColumnIndex(Key_FirstName);
int contactlname = c.getColumnIndex(Key_LastName);
int contactmobile = c.getColumnIndex(Key_Mobile);
int contactaddress = c.getColumnIndex(Key_Address);
// loop inside the DB starting by the first row//
String returnedcontacts = "";
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())// start from the
// beginning...not
// at the
// end..increment//
{
returnedcontacts = returnedcontacts + c.getString(contactrow) + " "
+ c.getString(contactfname) + " "
+ c.getString(contactlname) + " "
+ c.getString(contactmobile) + " "
+ c.getString(contactaddress) + " " + "\n";
}
return returnedcontacts;
}
}
视图类:
public class DisplayContacts extends Activity {
TextView contacts;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcontacts);
contacts = (TextView) findViewById(R.id.contactviewdata);
// display the contacts//
displaycontacts();
}
private void displaycontacts() {
// TODO Auto-generated method stub
DBgenerator view = new DBgenerator(DisplayContacts.this);
view.open();
String ContactsData = view.getallcontacts();
view.close();
contacts.setText(ContactsData);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
请帮忙,先生们
P.S。下面是我的LOGCAT
01-05 21:04:00.903: E/Trace(16448): error opening trace file: No such file or directory (2)
01-05 21:04:01.138: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:01.221: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:02.218: I/SurfaceTextureClient(16448): [0x4f06b390] frames:2, duration:1.042000, fps:1.918633
01-05 21:04:02.225: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:02.492: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:02.578: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:02.619: W/MMUMapper(16448): invalid operation for unregister MVA with VA(0x526dd000) size(614400) f(0x5)
01-05 21:04:02.620: W/MMUMapper(16448): invalid operation for unregister MVA with VA(0x52993000) size(614400) f(0x5)
01-05 21:04:02.621: W/MMUMapper(16448): invalid operation for unregister MVA with VA(0x52b3b000) size(614400) f(0x5)
01-05 21:04:03.033: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:03.657: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:4, duration:1.089000, fps:3.671077
01-05 21:04:04.768: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:3, duration:1.111000, fps:2.698411
01-05 21:04:05.844: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:4, duration:1.084000, fps:3.687304
01-05 21:04:07.112: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:4, duration:1.273000, fps:3.140990
01-05 21:04:08.240: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:3, duration:1.125000, fps:2.665755
01-05 21:04:09.600: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:5, duration:1.362000, fps:3.668667
01-05 21:04:10.667: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:5, duration:1.065000, fps:4.691889
01-05 21:04:11.673: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:4, duration:1.007000, fps:3.969711
01-05 21:04:13.133: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:7, duration:1.459000, fps:4.795339
01-05 21:04:14.171: I/SurfaceTextureClient(16448): [0x528c8ab0] frames:9, duration:1.037000, fps:8.675249
01-05 21:04:14.797: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:14.821: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:14.867: W/MMUMapper(16448): invalid operation for unregister MVA with VA(0x52bd1000) size(614400) f(0x5)
01-05 21:04:14.868: W/MMUMapper(16448): invalid operation for unregister MVA with VA(0x52c67000) size(614400) f(0x5)
01-05 21:04:14.868: W/MMUMapper(16448): invalid operation for unregister MVA with VA(0x526a7000) size(614400) f(0x5)
01-05 21:04:15.964: I/SurfaceTextureClient(16448): [0x5276e3d0] frames:2, duration:1.156000, fps:1.729373
01-05 21:04:15.968: W/MMUMapper(16448): fail to register MVA, unsupported format(0x5)
01-05 21:04:16.138: E/CursorWindow(16448): Failed to read row 0, column -1 from a CursorWindow which has 16 rows, 4 columns.
01-05 21:04:16.141: W/dalvikvm(16448): threadid=1: thread exiting with uncaught exception (group=0x419ff908)
01-05 21:04:16.154: E/AndroidRuntime(16448): FATAL EXCEPTION: main
01-05 21:04:16.154: E/AndroidRuntime(16448): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hossa.sqliteexample/com.hossa.sqliteexample.DisplayContacts}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2203)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.ActivityThread.access$600(ActivityThread.java:150)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.os.Looper.loop(Looper.java:153)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.ActivityThread.main(ActivityThread.java:5006)
01-05 21:04:16.154: E/AndroidRuntime(16448): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 21:04:16.154: E/AndroidRuntime(16448): at java.lang.reflect.Method.invoke(Method.java:511)
01-05 21:04:16.154: E/AndroidRuntime(16448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-05 21:04:16.154: E/AndroidRuntime(16448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-05 21:04:16.154: E/AndroidRuntime(16448): at dalvik.system.NativeStart.main(Native Method)
01-05 21:04:16.154: E/AndroidRuntime(16448): 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.
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.database.CursorWindow.nativeGetString(Native Method)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.database.CursorWindow.getString(CursorWindow.java:434)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-05 21:04:16.154: E/AndroidRuntime(16448): at com.hossa.sqliteexample.DBgenerator.getallcontacts(DBgenerator.java:97)
01-05 21:04:16.154: E/AndroidRuntime(16448): at com.hossa.sqliteexample.DisplayContacts.displaycontacts(DisplayContacts.java:23)
01-05 21:04:16.154: E/AndroidRuntime(16448): at com.hossa.sqliteexample.DisplayContacts.onCreate(DisplayContacts.java:16)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.Activity.performCreate(Activity.java:5076)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-05 21:04:16.154: E/AndroidRuntime(16448): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2167)
01-05 21:04:16.154: E/AndroidRuntime(16448): ... 11 more
答案 0 :(得分:0)
在getAllContacts中,您尝试通过游标访问_id(“Key_ROWID”)列,该列未包含在查询的列列表(“列”)中。无法找到列,索引返回(“contactrow”)为-1,访问抛出异常。将Key_ROWID添加到列,看看是否有效。
顺便说一句,您可以简单地写for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
而不是while( c.moveToNext() )
。
P.S。:getColumnIndex()的文档提供了一个有用的建议,我绝对是第二个:
如果您希望该列存在,请改用getColumnIndexOrThrow(String),这样可以使错误更加清晰。
因此,只要您控制列,select
就会使用后者。 (相反的可能是select * from ...
或您的应用程序无法控制的表格上的查询或可能属于其他版本的查询。“ContactsContract”任何人?)
答案 1 :(得分:0)
我很确定问题出在那段代码中:
.. c.getString(contactfname)
...
我担心contactfname
不是正确的列索引。尝试Log
您的查询,并显示每个结果。例如,我要尝试的第一件事是使用Log
检查列索引(contactfname,contactlname ...)。由于错误表明您正在尝试从错误的列索引中获取字符串(它不应该是-1)。您也可以尝试手动提供索引。