android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0 数据库错误
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 Database error
我做了一个数据库,节省了一些收入。但是当我尝试编辑时,它给了我这个错误。 android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0
这是我的activity。
public class IncomeActivity extends ActionBarActivity {
int from_Where_I_Am_Coming = 0;
private DBhelper mydb;
TextView payer;
TextView amount;
Spinner payments;
Spinner category;
int id_To_Update = 0;
private Calendar calendar;
private TextView dateView;
private int year, month, day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_income);
payer = (TextView) findViewById(R.id.input_payer);
amount = (TextView) findViewById(R.id.input_amount);
dateView = (TextView) findViewById(R.id.input_date);
dateView = (TextView) findViewById(R.id.input_date);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);
payments = (Spinner) findViewById(R.id.incomeNumber);
ArrayAdapter<CharSequence> payments_adapter = ArrayAdapter.createFromResource(this,
R.array.recurring_income, android.R.layout.simple_spinner_item);
payments_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
payments.setAdapter(payments_adapter);
category = (Spinner) findViewById(R.id.categoryIncome);
ArrayAdapter<CharSequence> income_adapter = ArrayAdapter.createFromResource(this,
R.array.category_income, android.R.layout.simple_spinner_item);
income_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(income_adapter);
mydb = new DBhelper(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
Cursor rs = mydb.getDataIncome(Value);
id_To_Update = Value;
rs.moveToFirst();
String amo = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_AMOUNT));
String pyr = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYER));
String dat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_DATE));
String pym = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYMENTS));
String cat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_CATEGORY));
if (!rs.isClosed()) {
rs.close();
}
Button save = (Button) findViewById(R.id.btn_save);
save.setVisibility(View.INVISIBLE);
Button cancel = (Button) findViewById(R.id.btn_cnc);
cancel.setVisibility(View.INVISIBLE);
amount.setText(amo);
amount.setFocusable(false);
amount.setClickable(false);
payer.setText(pyr);
payer.setFocusable(false);
payer.setClickable(false);
dateView.setText(dat);
dateView.setFocusable(false);
dateView.setClickable(false);
payments.setSelection(getIndex(payments, pym));
payments.setFocusable(false);
payments.setClickable(false);
category.setSelection(getIndex(category, cat));
category.setFocusable(false);
category.setClickable(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
getMenuInflater().inflate(R.menu.menu_income, menu);
} else {
getMenuInflater().inflate(R.menu.menu_main, menu);
}
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.Edit_Income:
Button save = (Button) findViewById(R.id.btn_save);
save.setVisibility(View.VISIBLE);
Button cancel = (Button) findViewById(R.id.btn_cnc);
cancel.setVisibility(View.VISIBLE);
amount.setEnabled(true);
amount.setFocusableInTouchMode(true);
amount.setClickable(true);
payer.setEnabled(true);
payer.setFocusableInTouchMode(true);
payer.setClickable(true);
dateView.setEnabled(true);
dateView.setFocusableInTouchMode(true);
dateView.setClickable(true);
payments.setEnabled(true);
payments.setFocusableInTouchMode(true);
payments.setClickable(true);
category.setEnabled(true);
category.setFocusableInTouchMode(true);
category.setClickable(true);
return true;
case R.id.Delete_Income:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteIncome)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteIncome(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void run(View view) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
if (mydb.updateIncome(id_To_Update, amount.getText().toString(), payer.getText().toString()
, dateView.getText().toString(), payments.getSelectedItem().toString(),
category.getSelectedItem().toString())) {
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
}
} else {
if (mydb.insertIncome(amount.getText().toString(), payer.getText().toString()
, dateView.getText().toString(), payments.getSelectedItem().toString(),
category.getSelectedItem().toString())) {
} else {
}
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
}
}
@SuppressWarnings("deprecation")
public void setDate(View view) {
showDialog(999);
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
return new DatePickerDialog(this, myDateListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
// arg1 = year
// arg2 = month
// arg3 = day
showDate(arg1, arg2+1, arg3);
}
};
private void showDate(int year, int month, int day) {
dateView.setText(new StringBuilder().append(day).append("/")
.append(month).append("/").append(year));
}
private int getIndex(Spinner spinner, String myString){
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).equals(myString)){
index = i;
}
}
return index;
}
}
这是我的logcat。
02-11 09:36:01.766 1366-1366/tubapps.datepickerdb E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tubapps.datepickerdb/tubapps.datepickerdb.IncomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access0(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at tubapps.datepickerdb.IncomeActivity.onCreate(IncomeActivity.java:74)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access0(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
尝试添加这个以检查光标是否返回 true
if (rs != null&& rs.moveToFirst()){
String amo = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_AMOUNT));
String pyr = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYER));
String dat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_DATE));
String pym = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYMENTS));
String cat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_CATEGORY));
}
您试图从 Cursor 读取内容,但它是空的,因此出现异常。
我做了一个数据库,节省了一些收入。但是当我尝试编辑时,它给了我这个错误。 android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0
这是我的activity。
public class IncomeActivity extends ActionBarActivity {
int from_Where_I_Am_Coming = 0;
private DBhelper mydb;
TextView payer;
TextView amount;
Spinner payments;
Spinner category;
int id_To_Update = 0;
private Calendar calendar;
private TextView dateView;
private int year, month, day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_income);
payer = (TextView) findViewById(R.id.input_payer);
amount = (TextView) findViewById(R.id.input_amount);
dateView = (TextView) findViewById(R.id.input_date);
dateView = (TextView) findViewById(R.id.input_date);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);
payments = (Spinner) findViewById(R.id.incomeNumber);
ArrayAdapter<CharSequence> payments_adapter = ArrayAdapter.createFromResource(this,
R.array.recurring_income, android.R.layout.simple_spinner_item);
payments_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
payments.setAdapter(payments_adapter);
category = (Spinner) findViewById(R.id.categoryIncome);
ArrayAdapter<CharSequence> income_adapter = ArrayAdapter.createFromResource(this,
R.array.category_income, android.R.layout.simple_spinner_item);
income_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(income_adapter);
mydb = new DBhelper(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
Cursor rs = mydb.getDataIncome(Value);
id_To_Update = Value;
rs.moveToFirst();
String amo = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_AMOUNT));
String pyr = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYER));
String dat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_DATE));
String pym = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYMENTS));
String cat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_CATEGORY));
if (!rs.isClosed()) {
rs.close();
}
Button save = (Button) findViewById(R.id.btn_save);
save.setVisibility(View.INVISIBLE);
Button cancel = (Button) findViewById(R.id.btn_cnc);
cancel.setVisibility(View.INVISIBLE);
amount.setText(amo);
amount.setFocusable(false);
amount.setClickable(false);
payer.setText(pyr);
payer.setFocusable(false);
payer.setClickable(false);
dateView.setText(dat);
dateView.setFocusable(false);
dateView.setClickable(false);
payments.setSelection(getIndex(payments, pym));
payments.setFocusable(false);
payments.setClickable(false);
category.setSelection(getIndex(category, cat));
category.setFocusable(false);
category.setClickable(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
getMenuInflater().inflate(R.menu.menu_income, menu);
} else {
getMenuInflater().inflate(R.menu.menu_main, menu);
}
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.Edit_Income:
Button save = (Button) findViewById(R.id.btn_save);
save.setVisibility(View.VISIBLE);
Button cancel = (Button) findViewById(R.id.btn_cnc);
cancel.setVisibility(View.VISIBLE);
amount.setEnabled(true);
amount.setFocusableInTouchMode(true);
amount.setClickable(true);
payer.setEnabled(true);
payer.setFocusableInTouchMode(true);
payer.setClickable(true);
dateView.setEnabled(true);
dateView.setFocusableInTouchMode(true);
dateView.setClickable(true);
payments.setEnabled(true);
payments.setFocusableInTouchMode(true);
payments.setClickable(true);
category.setEnabled(true);
category.setFocusableInTouchMode(true);
category.setClickable(true);
return true;
case R.id.Delete_Income:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteIncome)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteIncome(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void run(View view) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
if (mydb.updateIncome(id_To_Update, amount.getText().toString(), payer.getText().toString()
, dateView.getText().toString(), payments.getSelectedItem().toString(),
category.getSelectedItem().toString())) {
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
}
} else {
if (mydb.insertIncome(amount.getText().toString(), payer.getText().toString()
, dateView.getText().toString(), payments.getSelectedItem().toString(),
category.getSelectedItem().toString())) {
} else {
}
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
}
}
@SuppressWarnings("deprecation")
public void setDate(View view) {
showDialog(999);
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
return new DatePickerDialog(this, myDateListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
// arg1 = year
// arg2 = month
// arg3 = day
showDate(arg1, arg2+1, arg3);
}
};
private void showDate(int year, int month, int day) {
dateView.setText(new StringBuilder().append(day).append("/")
.append(month).append("/").append(year));
}
private int getIndex(Spinner spinner, String myString){
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).equals(myString)){
index = i;
}
}
return index;
}
}
这是我的logcat。
02-11 09:36:01.766 1366-1366/tubapps.datepickerdb E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tubapps.datepickerdb/tubapps.datepickerdb.IncomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access0(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at tubapps.datepickerdb.IncomeActivity.onCreate(IncomeActivity.java:74)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access0(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
尝试添加这个以检查光标是否返回 true
if (rs != null&& rs.moveToFirst()){
String amo = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_AMOUNT));
String pyr = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYER));
String dat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_DATE));
String pym = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_PAYMENTS));
String cat = rs.getString(rs.getColumnIndex(DBhelper.INCOME_COLUMN_CATEGORY));
}
您试图从 Cursor 读取内容,但它是空的,因此出现异常。