IllegalStateException异常:无法读取0行,列-1从cursorWindow [英] IllegalStateException: couldn't read row 0, col -1 from cursorWindow
问题描述
IllegalStateException异常:无法读取0行,列-1从cursorWindow。我是新到Android我看着smiller问题没有解决我的问题的帮助,请在此先感谢。
在这里我的code:
//函数中添加数据库数据
公共无效CreatExpenses(查看视图)
{
mCategory =(微调)findViewById(R.id.spCategory);
的EditText edittext2 =(EditText上)findViewById(R.id.AmountEditText);
的EditText edittext3 =(EditText上)findViewById(R.id.PayTypeEditText);
的EditText edittext4 =(EditText上)findViewById(R.id.DateEditText); 。类别= mCategory.getSelectedItem()的toString();
。金额= edittext2.getText()的toString();
。PaymentType = edittext3.getText()的toString();
。日期= edittext4.getText()的toString(); db.execSQL(INSERT INTO费用VALUES('+类别+,+数额+,+ PaymentType +','+日期+');); Toast.makeText(getBaseContext(), 您的信息保存成功!
Toast.LENGTH_SHORT).show();
} 公共无效ShowExpens(查看视图)
{
光标C = db.rawQuery(SELECT * FROM费用,NULL);
诠释计数= c.getCount();
c.moveToFirst();
TableLayout tableLayout =新TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(真);
的TableRow的TableRow;
TextView的TextView的,textView1,textView2,textView3,textView4,textView5,textView6;
的TableRow =新的TableRow(getApplicationContext());
TextView中=新的TextView(getApplicationContext());
textView.setText(Expense_Category);
textView.setTextColor(Color.RED);
textView.setTypeface(NULL,Typeface.BOLD);
textView.setPadding(20,20,20,20);
tableRow.addView(TextView的); textView4 =新的TextView(getApplicationContext());
textView4.setText(额);
textView4.setTextColor(Color.GREEN);
textView4.setTypeface(NULL,Typeface.BOLD);
textView4.setPadding(20,20,20,20);
tableRow.addView(textView4); textView5 =新的TextView(getApplicationContext());
textView5.setText(PaymentType);
textView5.setTextColor(Color.RED);
textView5.setTypeface(NULL,Typeface.BOLD);
textView5.setPadding(20,20,20,20);
tableRow.addView(textView5); textView6 =新的TextView(getApplicationContext());
textView6.setText(日期);
textView6.setTextColor(Color.RED);
textView6.setTypeface(NULL,Typeface.BOLD);
textView6.setPadding(20,20,20,20);
tableRow.addView(textView6); tableLayout.addView(tablerow的); 对于(整数j = 0; J<计数; J ++)
{
的TableRow =新的TableRow(getApplicationContext());
textView1 =新的TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex(Expense_Category)));
textView2 =新的TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex(额)));
textView3 =新的TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex(PaymentType)));
textView4 =新的TextView(getApplicationContext());
textView4.setText(c.getString(c.getColumnIndex(日期))); textView1.setPadding(20,20,20,20);
textView2.setPadding(20,20,20,20);
textView3.setPadding(20,20,20,20);
textView4.setPadding(20,20,20,20); tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableRow.addView(textView4); tableLayout.addView(tablerow的);
c.moveToNext();
}
的setContentView(tableLayout);
db.close();
}
公共无效关闭(查看视图)
{
System.exit(0);
}
}
这在LogCat中
9月11日至12日:50:01.978:E / CursorWindow(871):无法读取0行,列-1由具有1行4列的CursorWindow。
9月11日至一十二日:50:01.988:D / AndroidRuntime(871):关闭VM
9月11日至一十二日:50:01.988:W / dalvikvm(871):主题ID = 1:螺纹未捕获的异常(组= 0x41465700)退出
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):致命异常:主要
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):java.lang.IllegalStateException:无法执行活动的方法
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.view.View $ 1.onClick(View.java:3633)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.view.View.performClick(View.java:4240)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.view.View $ PerformClick.run(View.java:17721)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.os.Handler.handleCallback(Handler.java:730)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.os.Handler.dispatchMessage(Handler.java:92)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.os.Looper.loop(Looper.java:137)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.app.ActivityThread.main(ActivityThread.java:5103)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在java.lang.reflect.Method.invokeNative(本机方法)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在java.lang.reflect.Method.invoke(Method.java:525)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在dalvik.system.NativeStart.main(本机方法)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):由:java.lang.reflect.InvocationTargetException
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在java.lang.reflect.Method.invokeNative(本机方法)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在java.lang.reflect.Method.invoke(Method.java:525)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.view.View $ 1.onClick(View.java:3628)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):11 ...更多
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):java.lang.IllegalStateException:产生的原因无法读取0行,列-1从CursorWindow。确保光标从它访问数据之前正确初始化。
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.database.CursorWindow.nativeGetString(本机方法)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.database.CursorWindow.getString(CursorWindow.java:434)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):在com.info.househouldexpcalulator.MainActivity.ShowExpens(MainActivity.java:113)
9月11日至一十二日:50:02.148:E / AndroidRuntime(871):14 ...更多
9月11日至一十二日:50:02.308:D / dalvikvm(871):GC_FOR_ALLOC释放178K,8%的免费2841K / 3076K,暂停54ms,总89ms
您给的列名不存在。
getColumnIndex(字符串COLUMNNAME)
返回从零开始的索引为给定的列名,或-1,如果列不存在。
错误就在于你的任何code的以下部分:
c.getString(c.getColumnIndex(Expense_Category)); //通知Expense_Category后空间
c.getString(c.getColumnIndex(额));
c.getString(c.getColumnIndex(PaymentType));
c.getString(c.getColumnIndex(日期));
如果你期望存在列
使用getColumnIndexOrThrow(String)来代替,这将使错误更加清晰。
IllegalStateException: couldn't read row 0, col -1 from cursorWindow. I am new to android I looked at smiller question did solve my problem help please Thanks in advance.
here my code:
// function to add data in to db
public void CreatExpenses(View view)
{
mCategory = (Spinner )findViewById(R.id.spCategory);
EditText edittext2 = (EditText )findViewById(R.id.AmountEditText);
EditText edittext3 = (EditText )findViewById(R.id.PayTypeEditText);
EditText edittext4 = (EditText )findViewById(R.id.DateEditText);
category =mCategory.getSelectedItem().toString();
Amount =edittext2.getText().toString();
PaymentType =edittext3.getText().toString();
Date =edittext4.getText().toString();
db.execSQL("INSERT INTO Expenses VALUES('"+category+"','"+Amount+"','"+PaymentType+"','"+Date+"');");
Toast.makeText(getBaseContext(),
"Your info is saved successfully!",
Toast.LENGTH_SHORT).show();
}
public void ShowExpens(View view)
{
Cursor c=db.rawQuery("SELECT * from Expenses", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5,textView6;
tableRow = new TableRow(getApplicationContext());
textView=new TextView(getApplicationContext());
textView.setText("Expense_Category");
textView.setTextColor(Color.RED);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
textView4=new TextView(getApplicationContext());
textView4.setText("Amount");
textView4.setTextColor(Color.GREEN);
textView4.setTypeface(null, Typeface.BOLD);
textView4.setPadding(20, 20, 20, 20);
tableRow.addView(textView4);
textView5=new TextView(getApplicationContext());
textView5.setText("PaymentType");
textView5.setTextColor(Color.RED);
textView5.setTypeface(null, Typeface.BOLD);
textView5.setPadding(20, 20, 20, 20);
tableRow.addView(textView5);
textView6 = new TextView(getApplicationContext());
textView6.setText("Date");
textView6.setTextColor(Color.RED);
textView6.setTypeface(null, Typeface.BOLD);
textView6.setPadding(20, 20, 20, 20);
tableRow.addView(textView6);
tableLayout.addView(tableRow);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("Expense_Category ")));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("Amount")));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("PaymentType")));
textView4 = new TextView(getApplicationContext());
textView4.setText(c.getString(c.getColumnIndex("Date")));
textView1.setPadding(20, 20, 20, 20);
textView2.setPadding(20, 20, 20, 20);
textView3.setPadding(20, 20, 20, 20);
textView4.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableRow.addView(textView4);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
}
public void close(View view)
{
System.exit(0);
}
}
this the LogCat
11-12 09:50:01.978: E/CursorWindow(871): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 4 columns.
11-12 09:50:01.988: D/AndroidRuntime(871): Shutting down VM
11-12 09:50:01.988: W/dalvikvm(871): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-12 09:50:02.148: E/AndroidRuntime(871): FATAL EXCEPTION: main
11-12 09:50:02.148: E/AndroidRuntime(871): java.lang.IllegalStateException: Could not execute method of the activity
11-12 09:50:02.148: E/AndroidRuntime(871): at android.view.View$1.onClick(View.java:3633)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.view.View.performClick(View.java:4240)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.view.View$PerformClick.run(View.java:17721)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.os.Handler.handleCallback(Handler.java:730)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.os.Looper.loop(Looper.java:137)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-12 09:50:02.148: E/AndroidRuntime(871): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 09:50:02.148: E/AndroidRuntime(871): at java.lang.reflect.Method.invoke(Method.java:525)
11-12 09:50:02.148: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-12 09:50:02.148: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 09:50:02.148: E/AndroidRuntime(871): at dalvik.system.NativeStart.main(Native Method)
11-12 09:50:02.148: E/AndroidRuntime(871): Caused by: java.lang.reflect.InvocationTargetException
11-12 09:50:02.148: E/AndroidRuntime(871): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 09:50:02.148: E/AndroidRuntime(871): at java.lang.reflect.Method.invoke(Method.java:525)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.view.View$1.onClick(View.java:3628)
11-12 09:50:02.148: E/AndroidRuntime(871): ... 11 more
11-12 09:50:02.148: E/AndroidRuntime(871): 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.
11-12 09:50:02.148: E/AndroidRuntime(871): at android.database.CursorWindow.nativeGetString(Native Method)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.database.CursorWindow.getString(CursorWindow.java:434)
11-12 09:50:02.148: E/AndroidRuntime(871): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
11-12 09:50:02.148: E/AndroidRuntime(871): at com.info.househouldexpcalulator.MainActivity.ShowExpens(MainActivity.java:113)
11-12 09:50:02.148: E/AndroidRuntime(871): ... 14 more
11-12 09:50:02.308: D/dalvikvm(871): GC_FOR_ALLOC freed 178K, 8% free 2841K/3076K, paused 54ms, total 89ms
The column name you gave does not exists.
getColumnIndex(String columnName)
Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
Error lies in any of the following parts of your code:
c.getString(c.getColumnIndex("Expense_Category ")); //notice the space after Expense_Category
c.getString(c.getColumnIndex("Amount"));
c.getString(c.getColumnIndex("PaymentType"));
c.getString(c.getColumnIndex("Date"));
If you expect the column to exist use getColumnIndexOrThrow(String) instead, which will make the error more clear.
这篇关于IllegalStateException异常:无法读取0行,列-1从cursorWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!