Wednesday, February 12, 2014

About Cursor initialization problem


            
After calling AddTime add time and notes, click the Save button to jump back to the main page when an error

Tips
wrong title
 
08-07 12:31:13.469: E / CursorWindow (28703): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 3 columns
.08-07 12:31:13.519: E / AndroidRuntime (28703): FATAL EXCEPTION: main
08-07 12:31:13.519: E / AndroidRuntime (28703): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow Make sure the Cursor is initialized correctly before accessing data from it


 
package com.example.tracker;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

import com.example.tracker.TimeListDatabaseHelper;



public class MainActivity extends Activity {
private TimeTrackerAdapter timeTrackerAdapter;
private TimeListDatabaseHelper databaseHelper;
public static final String TIMETRACKER_COLUMN_TIME = "time";
public static final String TIMETRACKER_COLUMN_NOTES = "notes";
public static final int TIME_ENTRY_REQUEST_CODE = 1;


@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
databaseHelper = new TimeListDatabaseHelper (this);

ListView listView = (ListView) findViewById (R.id.times_list);
timeTrackerAdapter = new TimeTrackerAdapter (this, databaseHelper.getAllTimeRecords ());
listView.setAdapter (timeTrackerAdapter);

}

@ Override
public boolean onCreateOptionsMenu (Menu menu)
{
/ / Inflate the menu; this adds items to the action bar if it is present
.
MenuInflater menuInflater = getMenuInflater ();
menuInflater.inflate (R.menu.main, menu);
return super.onCreateOptionsMenu (menu);

}

@ Override
public boolean onMenuItemSelected (int featureId, MenuItem item) {
/ / TODO Auto-generated method stub
if (item.getItemId () == R.id.add_time_menu_item)
{
Intent intent = new Intent (this, AddTimeActivity.class);
startActivityForResult (intent, TIME_ENTRY_REQUEST_CODE);
return true;
}
return super.onOptionsItemSelected (item);
}

@ Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == TIME_ENTRY_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
String time = data.getStringExtra (TIMETRACKER_COLUMN_TIME);
String notes = data.getStringExtra (TIMETRACKER_COLUMN_NOTES);

databaseHelper.saveTimeRecord (time, notes);
timeTrackerAdapter.changeCursor (databaseHelper.getAllTimeRecords ());

}
}

}



}



 
package com.example.tracker;

import java.util.ArrayList;

import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TimeTrackerAdapter extends CursorAdapter {
public String a = "1";
public String b = "2";

public TimeTrackerAdapter (Context context, Cursor cursor)
{
super (context, cursor);
}
@ Override
public void bindView (View view, Context context, Cursor cursor)
{

TextView nameTextView = (TextView) view.findViewById (R.id.time_view);
nameTextView.setText (cursor.getString (cursor.getColumnIndex (a)));
TextView valueTextView = (TextView) view.findViewById (R.id.notes_view);
valueTextView.setText (cursor.getString (cursor.getColumnIndex (b)));
}
@ Override
public View newView (Context context, Cursor cursor, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from (parent.getContext ());
View view = inflater.inflate (R.layout.time_list_item, parent, false);
return view;
}

/ / Cursor cursor = databaseHelper.getAllTimeRecords ();
/ / If (cursor.moveToFirst ())
/ / Do
/ / {
/ / String time = cursor.getString (1);
/ / String notes = cursor.getString (2);
/ / Log.d ("DB Value", time + "" + notes);
/ /} While (cursor.moveToNext ());
/ / If (! Cursor.isClosed ())
/ / {
/ / Cursor.close ();
/ /}

}



 package com.example.tracker; 

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class TimeListDatabaseHelper {

private static final int DATABASE_VERSION = 11;
private static final String DATABASE_NAME = "timetracker.db";
private static final String TABLE_NAME = "timerecords";

public static final String TIMETRACKER_COLUMN_ID = "_id";
public static final String TIMETRACKER_COLUMN_TIME = "time";
public static final String TIMETRACKER_COLUMN_NOTES = "notes";

private TimeTrackerOpenHelper openHelper;
private SQLiteDatabase database;

public TimeListDatabaseHelper (Context context)
{
openHelper = new TimeTrackerOpenHelper (context);
database = openHelper.getWritableDatabase ();
}



public void saveTimeRecord (String time, String notes)
{

ContentValues ​​contentValues ​​= new ContentValues ​​();
contentValues.put (TIMETRACKER_COLUMN_TIME, time);
contentValues.put (TIMETRACKER_COLUMN_NOTES, notes);
database.insert (TABLE_NAME, null, contentValues);

}

public Cursor getAllTimeRecords ()
{
return database.rawQuery ("select * from" + TABLE_NAME, null);
}



private class TimeTrackerOpenHelper extends SQLiteOpenHelper
{
TimeTrackerOpenHelper (Context context)
{
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate (SQLiteDatabase database)
{
database.execSQL (
"CREATE TABLE" + TABLE_NAME + "("
+ TIMETRACKER_COLUMN_ID + "INTEGER PRIMARY KEY,"
+ TIMETRACKER_COLUMN_TIME + "TEXT,"
+ TIMETRACKER_COLUMN_NOTES + "TEXT)"
);
}

public void onUpgrade (SQLiteDatabase database, int oldVersion, int newVersion)
{
database.execSQL ("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate (database);
}
}



}



Reply:
I saw the cause of the error is
Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 3 columns.
LZ can find reasons to start from this section
Reply:
I know this, but this is not particularly understand, how to initialize it?
Reply:
reference to the second floor tianzecs reply:
I know this, but this is not particularly understand, how to initialize it?

You can see the program is NOT true Logcat throws, then traced down
I think
 TextView nameTextView = (TextView) view.findViewById (R.id.time_view); 
nameTextView.setText (cursor.getString (cursor.getColumnIndex (a)));
TextView valueTextView = (TextView) view.findViewById (R.id.notes_view);
valueTextView.setText (cursor.getString (cursor.getColumnIndex (b)));

Will your SQLite file is not listed in the table variable is not "1" or "2", so it returns the column -1
Reply:
reference to the third floor wangdong20 reply:
Quote: references to the second floor tianzecs reply:

I know this, but this is not particularly understand, how to initialize it?

You can see the program is NOT true Logcat throws, then traced down
I think
 TextView nameTextView = (TextView) view.findViewById (R.id.time_view); 
nameTextView.setText (cursor.getString (cursor.getColumnIndex (a)));
TextView valueTextView = (TextView) view.findViewById (R.id.notes_view);
valueTextView.setText (cursor.getString (cursor.getColumnIndex (b)));

Will your SQLite file is not listed in the table variable is not "1" or "2", so it returns the column -1



Has been resolved, I am in the process is a consequence of passing parameters in some places only lead to disunity, has now been resolved - thanks
Reply:
lz, your mistake is not in
nameTextView.setText (cursor.getString (cursor.getColumnIndex (a)));
, Set a changed TIMETRACKER_COLUMN_ID, and so on. . .
I encountered the same error, I do not know what the problem is, under the guidance of trouble, you're passing parameters is not uniform where ah? Thank you
Reply:
The landlord, how do you solve this problem, referring to the next, ah, ah thank you
Reply:
This may take the field you want to, not in the database, the situation did not get the.

No comments:

Post a Comment