在我的应用程序中,当我启动应用程序时,第一次一切正常,我可以在活动之间导航但是当回到第一个活动(MainActivity)并尝试转到第二个活动我的应用程序崩溃,我不知道为什么? !
在MainActivity中:
img_khayam.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(MainActivity.this,KhayamSeason.class); MainActivity.this.startActivity(intent); } });在第二项活动(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(); } }在DataBase类中:
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 ; }正如我第一次说的那样,当我在MainActivity和第二个活动(KhayamSeason)之间导航时没有问题但是当我按下后退按钮并再试一次时我的应用程序将崩溃。 崩溃后,当我看到Logcat时,它引用了我这些代码行:
Name[i] = db.Show_Season("khayam", i); Id[i] = db.get_id2("khayam", i, 0);但如果这两条线有问题,为什么第一次工作得很好?! 我的应用程序实际设计用于显示第二个活动中的khayam诗歌季节列表,并且使用名为“khayam”的表格保存在SQLite数据库中,并且我的第一个字段是“ID”,第二个“名称”和第三个“文本”。
我的问题在哪里? 或者如何为第二个活动设置默认值? 因为我认为按下后退按钮后仍有一些数据。
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.
最满意答案
最后,我通过使用关闭游标更改数据库类来解决我的问题:
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 ; }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 ; }更多推荐
发布评论