0

In my app when I launch the app, for the first time everything work well and I can navigate between activities but when come back to first activity(MainActivity) and try to go to second activity my app crashed and I don't know why?!

In MainActivity:

    img_khayam.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(MainActivity.this,KhayamSeason.class);
        MainActivity.this.startActivity(intent);

    }
});

In second activity(KhayamSeason):

        package com.xalid.khayam;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class KhayamSeason extends ListActivity {
    private database db;
    private String[] Name;
    private String[] Id;
    private int i =0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seasone);
        db = new database(this);
        refresher();
        setListAdapter(new AA());
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(KhayamSeason.this, ShowText.class);
        //intent.putExtra("name", Name[position]);
        intent.putExtra("id", Id[position]);
        KhayamSeason.this.startActivity(intent);
    }

    class AA extends ArrayAdapter<String> {

        public AA() {
            super(KhayamSeason.this, R.layout.raw_seasone, Name);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater in = getLayoutInflater();
            View row = in.inflate(R.layout.raw_seasone, parent, false);
            TextView name = (TextView) row.findViewById(R.id.name_poet);
            name.setText(Name[position]);
            name.setTypeface(MainActivity.font);
            return (row);
        }

    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();


    }

    private void refresher() {
        db.open();
        int save = db.Count_field("khayam");
        Name = new String[save];
        Id = new String[save];
        for (i = 0; i < save; i++) {
            Name[i] = db.Show_Season("khayam", i);
            Id[i] = db.get_id2("khayam", i, 0);
        }
        db.close();

    }
}

In DataBase class:

       public Integer Count_field(String table){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by ID", null);
        int i = Cursor.getCount();
        return i ;
    }

  public String Show_Season(String table , int row){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by ID", null);
        Cursor.moveToPosition(row);
        String s = Cursor.getString(1);
        return s ;
    }
    public String get_id2(String table,int field, int col){
        Cursor Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by ID", null);
        Cursor.moveToPosition(field);
        String s = Cursor.getString(col);
        return s ;
    }

As I say for the first time there is no problem when I navigate between MainActivity and second activity(KhayamSeason) but when I press back button and try again my app will crash. After crash when I look at Logcat it refer me to these to line of code:

 Name[i] = db.Show_Season("khayam", i);
        Id[i] = db.get_id2("khayam", i, 0);

But if these two line has a problem why for the first time work well?! My app actually design for showing a list of khayam's poem season in second activity and names saved in SQLite database with table named "khayam" and my firs field is "ID" and second "name" and third "text".

where is my problem? Or how set default values for second activity? because I think some data remain after I press back button.

  • 1
    Crash > no Logcat posted ?? – 2Dee Feb 18 '15 at 09:19
  • Logcat just show me where is it error – Khaled Rostampour Feb 18 '15 at 09:22
  • 1
    "I have an error, but Logcat only shows me the error, why would I need to know about an error when I have an error?" -> You're not making any sense ... – 2Dee Feb 18 '15 at 09:24
  • I don't know how tell you but my problem is why for the first time everything work well but for second time not? – Khaled Rostampour Feb 18 '15 at 09:26
  • 1
    *But if these two line has a problem why for the first time work well?!* - exactly why you should post the logcat as it will tell you why, what is causing it – Pararth Feb 18 '15 at 09:29
  • error: at com.xalid.khayam.KhayamSeason.refresher(KhayamSeason.java:78) – Khaled Rostampour Feb 18 '15 at 09:33
  • post whole log cat or atleast if it's line 78 then point out that line. – Anirudh Sharma Feb 18 '15 at 09:35
  • I find this too:android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=761 (# cursors opened by this proc=761) – Khaled Rostampour Feb 18 '15 at 09:40
  • looks like your `for` loop is the problem, value of `save` changes the 2nd time you arrive there, and `i` would always be 0, check the logcat for `caused by` line – Pararth Feb 18 '15 at 09:41
  • I found cause by: Caused by: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=763 (# cursors opened by this proc=763) – Khaled Rostampour Feb 18 '15 at 09:44
  • possible duplicate of [android.database.CursorWindowAllocationException when moving a Cursor](http://stackoverflow.com/questions/21219039/android-database-cursorwindowallocationexception-when-moving-a-cursor) – Pararth Feb 18 '15 at 09:51
  • http://stackoverflow.com/questions/21219039/android-database-cursorwindowallocationexception-when-moving-a-cursor – Pararth Feb 18 '15 at 09:51
  • It's seem to be my answer but how close the cursor? – Khaled Rostampour Feb 18 '15 at 09:56

1 Answers1

0

Finally I solve my problem by changing in database class by using closing the cursor:

     public String get_id2(String table,int field, int col){


        Cursor Cursor=null;
       try {
         Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by ID", null);
         Cursor.moveToPosition(field);
        String s = Cursor.getString(col);
        return s ;
        } catch (Exception e) {

        } finally {
           if(Cursor!= null){
               Cursor.close();
           }
        }
    return null ;
    }
 public Integer Count_field(String table){
        Cursor cu=null;
        int i;
           try {
                cu = mydb.rawQuery("SELECT * FROM "+table+" group by ID", null);
                i = cu.getCount();
                return i;
            } catch (Exception e) {

            } finally {
               if(cu!= null){
                   cu.close();
               }
            }
        return null ;
    }
    public String Show_Season(String table , int row){


        Cursor Cursor=null;
           try {
               Cursor = mydb.rawQuery("SELECT * FROM "+table+" group by ID", null);
               Cursor.moveToPosition(row);
                String s = Cursor.getString(1);
                return s ;
            } catch (Exception e) {

            } finally {
               if(Cursor!= null){
                   Cursor.close();
               }
            }
        return null ;

    }