我有一些字串,我在我的第一個活動中寫入 Edittext,我試圖通過按 Button 將它發送到另一個活動的 Edittext,但它給了我一個錯誤“java.lang.RuntimeException:無法啟動活動 ComponentInfo{ com.example.kp_orginizer/com.example.kp_orginizer.calendar}:android.database.CursorIndexOutOfBoundsException:請求索引 0,大小為 0”
有人可以幫我嗎
這是我在第一個 Activity 上的 onCLick 方法:
public void calendarOR(View v, AdapterView<?> parent, int position, long id) {
String data = cala.getText().toString();
Intent intent = new Intent(this, calendar.class);
intent.putExtra("id",data);
startActivity(intent);
我的第二種方法:
Bundle extras = getIntent().getExtras();
if (extras != null) {
userId = extras.getString("id");
}
if (userId !=null) {
userCursor = db.rawQuery("select * from " dbelper.TABLE " where " dbelper.COLUMN_ID "=?", new String[]{userId});
userCursor.moveToFirst();
date.setText(userCursor.getString(1));
userCursor.close();
}
它抱怨“date.setText(userCursor.getString(1));” 細繩
這是一個錯誤代碼:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kp_orginizer, PID: 7787
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kp_orginizer/com.example.kp_orginizer.calendar}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:515)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:138)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:52)
at com.example.kp_orginizer.calendar.onCreate(calendar.java:94)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
這是我填充資料庫的方式:
public void save(View view) {
ContentValues cv = new ContentValues();
int idishnik = 1;
cv.put(dbelper.COLUMN_NOTE, note.getText().toString());
cv.put(dbelper.COLUMN_DATE, cala.getText().toString());
cv.put(dbelper.ID_TABLITSY, idishnik);
if (userId > 0) {
db.update(dbelper.TABLE, cv, dbelper.COLUMN_ID "=" userId, null);
} else {
db.insert(dbelper.TABLE, null, cv);
}
goHome();
}
uj5u.com熱心網友回復:
看起來您傳遞userId的不是有效/存在的資料庫密鑰。如果您只想顯示您在 EditText 中傳遞的內容,而不是使用它從資料庫中獲取某些內容,您只需執行以下操作
if (userId != null) {
date.setText(userId);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394747.html
上一篇:如何使用`GROUPBY`在單個查詢中找到SQL中最高和第二高的條目?
下一篇:MySQL-殺死所有休眠的連接
