1
我在做一個Android應用程序,我想在其中添加一個新用戶來實現註冊功能。無法讀取來自CursorWindow的行
起初,我創建了一個稱爲註冊的活動,並且當按下按鈕,它調用添加用戶的方法:
public User createUser(String username, String password, String email) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelperUsers.COLUMN_USERNAME, username);
values.put(MySQLiteHelperUsers.COLUMN_PASSWORD, password);
values.put(MySQLiteHelperUsers.COLUMN_EMAIL, email);
long insertId = database.insert(MySQLiteHelperUsers.TABLE_USERS, null,
values);
Cursor cursor = database.query(MySQLiteHelperUsers.TABLE_USERS,
allColumns, MySQLiteHelperUsers.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
User newUser = cursorToUser(cursor);
cursor.close();
return newUser;
}
這裏是cursorToUser方法:
private User cursorToUser(Cursor cursor) {
User user = new User();
user.setId(cursor.getLong(cursor.getColumnIndex("_id")));
user.setUsername(cursor.getString(cursor.getColumnIndex("_username")));
user.setPassword(cursor.getString(cursor.getColumnIndex("_password")));
user.setEmail(cursor.getString(cursor.getColumnIndex("_email")));
return user;
}
但它給了我這個錯誤:
05-10 13:09:35.081: E/AndroidRuntime(16380): FATAL EXCEPTION: main
05-10 13:09:35.081: E/AndroidRuntime(16380): java.lang.IllegalStateException: Could not execute method of the activity
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.view.View$1.onClick(View.java:3704)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.view.View.performClick(View.java:4232)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.view.View$PerformClick.run(View.java:17298)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.os.Handler.handleCallback(Handler.java:615)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.os.Looper.loop(Looper.java:137)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.app.ActivityThread.main(ActivityThread.java:4921)
05-10 13:09:35.081: E/AndroidRuntime(16380): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 13:09:35.081: E/AndroidRuntime(16380): at java.lang.reflect.Method.invoke(Method.java:511)
05-10 13:09:35.081: E/AndroidRuntime(16380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
05-10 13:09:35.081: E/AndroidRuntime(16380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
05-10 13:09:35.081: E/AndroidRuntime(16380): at dalvik.system.NativeStart.main(Native Method)
05-10 13:09:35.081: E/AndroidRuntime(16380): Caused by: java.lang.reflect.InvocationTargetException
05-10 13:09:35.081: E/AndroidRuntime(16380): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 13:09:35.081: E/AndroidRuntime(16380): at java.lang.reflect.Method.invoke(Method.java:511)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.view.View$1.onClick(View.java:3699)
05-10 13:09:35.081: E/AndroidRuntime(16380): ... 11 more
05-10 13:09:35.081: E/AndroidRuntime(16380): 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.
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.database.CursorWindow.nativeGetLong(Native Method)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.database.CursorWindow.getLong(CursorWindow.java:511)
05-10 13:09:35.081: E/AndroidRuntime(16380): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
05-10 13:09:35.081: E/AndroidRuntime(16380): at dao.UsersDataSource.cursorToUser(UsersDataSource.java:75)
05-10 13:09:35.081: E/AndroidRuntime(16380): at dao.UsersDataSource.createUser(UsersDataSource.java:44)
05-10 13:09:35.081: E/AndroidRuntime(16380): at com.example.pocket_city.Registration.registrationClick(Registration.java:47)
05-10 13:09:35.081: E/AndroidRuntime(16380): ... 14 more
05-10 13:09:35.141: D/dalvikvm(16380): GC_CONCURRENT freed 177K, 14% free 9641K/11143K, paused 15ms+4ms, total 79ms
這裏是MySQLiteHelperUsers類:
public class MySQLiteHelperUsers extends SQLiteOpenHelper{
public static final String TABLE_USERS = "users";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_USERNAME = "_username";
public static final String COLUMN_PASSWORD = "_password";
public static final String COLUMN_EMAIL = "_email";
private static final String DATABASE_NAME = "users.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "CREATE TABLE "
+ TABLE_USERS.trim() + "("
+ COLUMN_ID.trim() + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_USERNAME.trim() + " TEXT NOT NULL, "
+ COLUMN_PASSWORD.trim() + " TEXT NOT NULL, "
+ COLUMN_EMAIL.trim()
+ " TEXT NOT NULL)";
public MySQLiteHelperUsers(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
註冊活動:
public class Registration extends Activity {
UsersDataSource usersDataSource = null;
User user = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
usersDataSource = new UsersDataSource(getApplicationContext());
usersDataSource.open();
}
public void registrationClick(View view){
EditText editUsername = (EditText) findViewById(R.id.editUsername);
String username = editUsername.getText().toString();
EditText editPassw = (EditText) findViewById(R.id.editPassw);
String passw = editPassw.getText().toString();
EditText editPassw2 = (EditText) findViewById(R.id.editPassw2);
String passw2 = editPassw2.getText().toString();
EditText editEmail = (EditText) findViewById(R.id.editEmail);
String email = editEmail.getText().toString();
EditText editEmail2 = (EditText) findViewById(R.id.editEmail2);
String email2 = editEmail2.getText().toString();
System.out.println("PASSW= "+passw+" "+passw2);
System.out.println("EMAIL= "+email+" "+email2);
/*if(!passw.equals(passw2) && !email.equals(email2)){
Toast.makeText(getApplicationContext(), "Le password/email non sono uguali!", Toast.LENGTH_LONG).show();
}*/
usersDataSource.createUser(username, passw, email);
/*Toast.makeText(getApplicationContext(), "Registrazione riuscita!", Toast.LENGTH_LONG).show();*/
}
}
的錯誤似乎更多地與單擊處理程序和活動。與我們分享。 – tbkn23 2013-05-10 11:21:44
我已添加註冊活動 – DamianFox 2013-05-10 11:30:26