IllegalStateException:无法从cursorWindow读取第0行,col -1

时间:2014-11-12 15:25:29

标签: android sqlite cursor

IllegalStateException:无法从cursorWindow读取第0行,col -1。我是新手android我看了smiller问题确实解决了我的问题帮助请提前致谢。

这里是我的代码:

// 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);
    }
}

这是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

1 个答案:

答案 0 :(得分:0)

您提供的列名称不存在。

getColumnIndex(String columnName)

返回给定列名的从零开始的索引,如果列不存在,则返回-1。
错误在于代码的以下任何部分:

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"));

如果您希望该列存在  改为使用getColumnIndexOrThrow(String),这将使错误更加清晰。