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.