未能第二次打开活动

时间:2015-02-18 09:17:35

标签: android database android-activity

在我的应用程序中,当我启动应用程序时,第一次一切正常并且我可以在活动之间导航但是当回到第一个活动(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”,第二个“名称”和第三个“文本”。

我的问题在哪里? 或者如何设置第二个活动的默认值?因为我认为按下按钮后仍有一些数据存在。

1 个答案:

答案 0 :(得分:0)

最后,我通过使用关闭游标来更改数据库类来解决我的问题:

     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 ;

    }