vendredi 22 avril 2016

hello why is my app crushing cause of " Failed to read row 0 column -1 from a CursorWindow which has 4 rows 5 columns."

i am creating an app with four columns using a gridview but its crusing returning an error i mentioned earlier. the app has three activities, the course_score.java, name_class.java and the database class.

this is the database where entries are stored StudentDataBase.java

public class StudentDataBase{
public static final String KEY_ROWID = "_id";

   public static final String KEY_STUDENT_NAME = "student_name";
public static final String KEY_CLASS = "class";
public static final String KEY_COURSE = "course";
public static final String KEY_SCORE = "score";

// database name
public static final String DATABASE_NAME = 

"StudentDataBase";
// database table
public static final String DATABASE_TABLE = "EntriesTable";

public static final int DATABASE_VERSION = 1;
public static final String TABLE_CREATE=" create table " + 

 DATABASE_TABLE + "(  " + KEY_ROWID
                + " INTEGER PRIMARY KEY 

AUTOINCREMENT, " + KEY_STUDENT_NAME + " TEXT, " + KEY_CLASS + " 

TEXT, " + KEY_COURSE + " TEXT, "
                + KEY_SCORE + " TEXT );";  


private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private ContentProvider getProvider;

private static class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, 

     DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        // the current version.
        db.execSQL(TABLE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int 

     oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        // one
        db.execSQL(" DROP TABLE IF EXITS " + 

     DATABASE_TABLE);
        onCreate(db);
    }

}

   public CashFlowDataBase(Context c) {
    // Context for the applications using the database.

    ourContext = c;

   }

   public CashFlowDataBase open() {
    // Database open/upgrade helper
    ourHelper = new DBHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
   }

   public void close() {
    // closing our helper
    ourHelper.close();
   }


  public long name_Class(String student_name, String class) {
    // TODO Auto-generated method stub

    ContentValues cvIt = new ContentValues();
    cvIt.put(KEY_STUDENT_NAME, student_name);
    cvIt.put( KEY_CLASS , class);
    return ourDatabase.insertOrThrow(DATABASE_TABLE, 
    null, cvIt);

    }

   public long course_score(String  course, String score) {
    // TODO Auto-generated method stub
    ContentValues vc = new ContentValues();
    vc.put(KEY_COURSE, course);
    vc.put(KEY_SCORE, score);
    return ourDatabase.insertOrThrow(DATABASE_TABLE, 
   null, vc);
   }
   public Cursor DisplayData() {
    // TODO Auto-generated method stub
    //selectQuery
    return ourDatabase.rawQuery("SELECT * FROM 
    EntriesTable", null);
    }

Name_Class.java this is one of the classes/activities that store data in the studentdatabase.

  public class Name_Class extends Activity implements OnClickListener{

  SimpleAdapter adapter;
 Button btn_xp;
  EditText sqlname, sqlclass;


   StudentDataBase entry;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags
 (WindowManager.LayoutParams.FLAG_FULLSCREEN,
   WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.name_class);
    inivaris();
   }

  private void inivaris() {
    btn_ncp = (Button) findViewById(R.id.button1);
    sqlname = (EditText) findViewById(R.id.etsqname);
    sqlclass = (EditText) findViewById(R.id.etsqlclass);
    btn_xp.setOnClickListener(this);

    }

   @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:


        String name = sqlname.getText().toString().trim();
        String class = sqlclass.getText().toString().trim();
        entry = new CashFlowDataBase(getBaseContext ());        
        try {
          StudentDataBase(Name_Class.this);

        entry.open();
        long id = entry.name_Class(name, class);                
        }catch (Exception e) {
         e.printStackTrace();           
        }finally{
        sqlname.setText("");
        sqlclass.setText("");
        entry.close();      
        }

    break;  
    }
     }

Course_score.java this is the second class that stores entries in the database.

            public class Course_score extends Activity implements 

              OnClickListener {

            private TextView tv_time;
        private EditText et_course;
         private Button btn_kb;
           studentDatabase entry;
             private EditText et_score ;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags
     (WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.coursendscore);


      et_course = (EditText) findViewById(R.id.et_course);
    et_score = (EditText) findViewById(R.id.et_score);


     btn_save = (Button) findViewById(R.id.btn_kb);




         }

        @Override
         public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btn_kb:

        String course = et_course.getText
            ().toString().trim();
        String score = et_score.getText().toString
          ().trim();
        entry = new StudentDataBase(getBaseContext());


        try {
            entry.open();
            long id = entry.name_Class

            (student_name, class);

        } catch (Exception e) {

            e.printStackTrace();
        } finally {

            // clean up for new input
            et_course.setText("");
            et_score.setText("");
            entry.close();
        }
        Toast.makeText(this, "Successfully 

                inserted", Toast.LENGTH_SHORT)
                .show();
        break;

    }

}

Aucun commentaire:

Enregistrer un commentaire