好的...我遇到了问题,当我运行我的应用程序时,出现错误无法从游标窗口读取第0行,我完全感到困惑,有人可以为我解释一下此问题。

谢谢。


package com.example.activitytracker02;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Scheduler extends Activity {

String ddate,valueActivity,dduration,sstime,fftime;
private Spinner activitySpinner;
SQLiteDatabase db;
 TableRow tableRow;
   TextView textView,textView1,textView2,textView3,textView4,textView5,textView6,textView7,textView8,textView9;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scheduler);
    db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Scheduler101(valueDay VARCHAR,valueActivity VARCHAR,dduration VARCHAR,sstime VARCHAR,fftime VARCHAR);");
}

public void Submit(View view)
{
  EditText edittext1=(EditText )findViewById(R.id.editText1);
  EditText edittext2=(EditText )findViewById(R.id.editText2);
  EditText edittext3=(EditText )findViewById(R.id.editText3);
  EditText edittext4=(EditText )findViewById(R.id.editText4);

  activitySpinner = (Spinner)findViewById(R.id.activitySpinner);
  dduration=edittext1.getText().toString();
  sstime=edittext2.getText().toString();
  fftime=edittext3.getText().toString();
  ddate=edittext4.getText().toString();
  valueActivity = String.valueOf(activitySpinner.getSelectedItem());
  db.execSQL("INSERT INTO  Scheduler101 VALUES('"+ddate+"','"+valueActivity+"','"+dduration+"','"+sstime+"','"+fftime+"');");
}

public void Show(View view)
{
    Cursor c=db.rawQuery("SELECT * from Scheduler101", 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,textView7,textView8,textView9;
   tableRow = new TableRow(getApplicationContext());
   textView=new TextView(getApplicationContext());
   textView.setText("Date(DD/MM/YYYY)");
   textView.setTextColor(Color.RED);
    textView.setTypeface(null, Typeface.BOLD);
     textView.setPadding(20, 20, 20, 20);
    tableRow.addView(textView);
    textView4=new TextView(getApplicationContext());
    textView4.setText("Activity");
    textView4.setTextColor(Color.RED);
    textView4.setTypeface(null, Typeface.BOLD);
     textView4.setPadding(20, 20, 20, 20);
    tableRow.addView(textView4);
    textView5=new TextView(getApplicationContext());
    textView5.setText("Duration(Minutes)");
    textView5.setTextColor(Color.RED);
    textView5.setTypeface(null, Typeface.BOLD);
    textView5.setPadding(20, 20, 20, 20);
    tableRow.addView(textView5);
    textView6=new TextView(getApplicationContext());
    textView6.setText("Start Time");
    textView6.setTextColor(Color.RED);
    textView6.setTypeface(null, Typeface.BOLD);
     textView6.setPadding(20, 20, 20, 20);
    tableRow.addView(textView6);
    textView7=new TextView(getApplicationContext());
    textView7.setText("Finish Time");
    textView7.setTextColor(Color.RED);
    textView7.setTypeface(null, Typeface.BOLD);
     textView7.setPadding(20, 20, 20, 20);
    tableRow.addView(textView7);
   tableLayout.addView(tableRow);
     for (Integer j = 0; j < count; j++)
     {
         tableRow = new TableRow(getApplicationContext());
         textView1 = new TextView(getApplicationContext());
         textView1.setText(c.getString(c.getColumnIndex("ddate")));
         textView2 = new TextView(getApplicationContext());
         textView2.setText(c.getString(c.getColumnIndex("valueActivity")));
         textView3 = new TextView(getApplicationContext());
         textView3.setText(c.getString(c.getColumnIndex("dduration")));
         textView8 = new TextView(getApplicationContext());
         textView8.setText(c.getString(c.getColumnIndex("sstime")));
         textView9 = new TextView(getApplicationContext());
         textView9.setText(c.getString(c.getColumnIndex("fftime")));
         textView1.setPadding(20, 20, 20, 20);
         textView2.setPadding(20, 20, 20, 20);
         textView3.setPadding(20, 20, 20, 20);
         textView8.setPadding(20, 20, 20, 20);
         textView9.setPadding(20, 20, 20, 20);
         tableRow.addView(textView1);
         tableRow.addView(textView2);
         tableRow.addView(textView3);
         tableRow.addView(textView8);
         tableRow.addView(textView9);
         tableLayout.addView(tableRow);
         c.moveToNext() ;
     }
     setContentView(tableLayout);
db.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.scheduler, menu);
    return true;
}
@Override
 public boolean onOptionsItemSelected(MenuItem item)
 {

 switch (item.getItemId())
 {
 case R.id.menu_next:
     Intent a =new Intent(Scheduler.this, ViewActivities.class);
        startActivityForResult(a,0);
 case R.id.menu_back:
     Intent b =new Intent(Scheduler.this, AddNewProfile.class);
        startActivityForResult(b,0);
 default:
 return super.onOptionsItemSelected(item);
 }

   }
 }

错误信息:
12-17 21:14:53.311: E/CursorWindow(3198): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 5 columns.
12-17 21:14:53.311: D/AndroidRuntime(3198): Shutting down VM
12-17 21:14:53.311: W/dalvikvm(3198): threadid=1: thread exiting with uncaught exception (group=0xb3ac5b90)
12-17 21:14:53.371: E/AndroidRuntime(3198): FATAL EXCEPTION: main
12-17 21:14:53.371: E/AndroidRuntime(3198): Process: com.example.activitytracker02, PID: 3198
12-17 21:14:53.371: E/AndroidRuntime(3198): java.lang.IllegalStateException: Could not execute method of the activity
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.view.View$1.onClick(View.java:3814)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.view.View.performClick(View.java:4424)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.view.View$PerformClick.run(View.java:18383)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.os.Handler.handleCallback(Handler.java:733)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.os.Looper.loop(Looper.java:137)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at java.lang.reflect.Method.invoke(Method.java:515)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at dalvik.system.NativeStart.main(Native Method)
12-17 21:14:53.371: E/AndroidRuntime(3198): Caused by: java.lang.reflect.InvocationTargetException
12-17 21:14:53.371: E/AndroidRuntime(3198):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at java.lang.reflect.Method.invoke(Method.java:515)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.view.View$1.onClick(View.java:3809)
12-17 21:14:53.371: E/AndroidRuntime(3198):     ... 11 more
12-17 21:14:53.371: E/AndroidRuntime(3198): 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.
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.database.CursorWindow.nativeGetString(Native Method)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.database.CursorWindow.getString(CursorWindow.java:434)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
12-17 21:14:53.371: E/AndroidRuntime(3198):     at com.example.activitytracker02.Scheduler.Show(Scheduler.java:96)
12-17 21:14:53.371: E/AndroidRuntime(3198):     ... 14 more
12-17 21:19:53.671: I/Process(3198): Sending signal. PID: 3198 SIG: 9
12-17 21:19:59.871: D/dalvikvm(3303): GC_FOR_ALLOC freed 51K, 5% free 2850K/2972K, paused 43ms, total 45ms
12-17 21:20:00.171: D/dalvikvm(3303): GC_FOR_ALLOC freed 49K, 4% free 3298K/3424K, paused 38ms, total 41ms
12-17 21:20:00.671: D/(3303): HostConnection::get() New Host Connection established 0xb724b0a8, tid 3303
12-17 21:20:00.981: W/EGL_emulation(3303): eglSurfaceAttrib not implemented
12-17 21:20:01.001: D/OpenGLRenderer(3303): Enabling debug mode 0
12-17 21:30:18.551: D/dalvikvm(3348): GC_FOR_ALLOC freed 55K, 5% free 2850K/2976K, paused 43ms, total 45ms
12-17 21:30:18.801: D/dalvikvm(3348): GC_FOR_ALLOC freed 49K, 4% free 3297K/3416K, paused 32ms, total 34ms
12-17 21:30:19.081: D/(3348): HostConnection::get() New Host Connection established 0xb7107c28, tid 3348
12-17 21:30:19.381: W/EGL_emulation(3348): eglSurfaceAttrib not implemented
12-17 21:30:19.401: D/OpenGLRenderer(3348): Enabling debug mode 0
12-17 21:30:30.951: W/EGL_emulation(3348): eglSurfaceAttrib not implemented

最佳答案

尝试更改:

textView1.setText(c.getString(c.getColumnIndex("ddate")));

至:
textView1.setText(c.getString(c.getColumnIndex("valueDay")));

关于android - 无法从CursorWindow读取第0行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20648792/

10-10 01:41