1

我的应用程序中有一个数据库,我使用一个表单使用 edittext 输入数据,完成后,用户将单击提交。我单击我之前输入的列表数据之一到另一个活动以显示之前输入的数据,它强制关闭。我不知道我的错在哪里,这是我的数据库:

        class DBSpaj extends SQLiteOpenHelper {
        private static final String DATABASE_NAME="dbspaj.db";
        private static final int SCHEMA_VERSION=1;

        public DBSpaj(Context context) {
            super(context, DATABASE_NAME, null, SCHEMA_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("DROP TABLE IF EXISTS table_spaj");
            db.execSQL("CREATE TABLE if not exists table_spaj (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    " agama TEXT,"+
                    " agamatt TEXT,"+
                    " alamat TEXT,"+
                    " alamat_tt TEXT,"+
                    " alamat_tagihan TEXT,"+
                    " alamat_tagihan_tt TEXT,"+
                    " alamatkantor TEXT,"+
                    " curenttime TEXT," +
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // no-op, since will not be called until 2nd schema
            // version exists
        }

        public Cursor getAll() {
            return(getReadableDatabase()
                            .rawQuery("SELECT _id, agama,agamatt,alamat,alamat_tt,alamat_tagihan,alamat_tagihan_tt,alamatkantor,curenttime FROM table_spaj ORDER BY edit_nama_pp", null));

        }

        public void insert(
                String agama,
                String agamatt,
                String alamat,
                String alamat_tt,
                String alamat_tagihan,
                String alamat_tagihan_tt,
                String alamatkantor,
                String curenttime
                ) {
            ContentValues cv=new ContentValues();

            cv.put("agama", agama);
            cv.put("agamatt", agamatt);
            cv.put("alamat", alamat);
            cv.put("alamat_tt", alamat_tt);
            cv.put("alamat_tagihan", alamat_tagihan);
            cv.put("alamat_tagihan_tt", alamat_tagihan_tt);
            cv.put("alamatkantor", alamatkantor);
            cv.put("curenttime", GetTime.getCurrentDate("yyyyMMddhhmmss"));

            getWritableDatabase().insert("table_spaj", "name", cv);
        }

        public String getagama(Cursor c) {
            return(c.getString(1));
        }

        public String getagamatt(Cursor c) {
            return(c.getString(2));
        }

        public String getalamat(Cursor c) {
            return(c.getString(3));
        }

    public String getalamat_tt(Cursor c) {
            return(c.getString(4));
        }

    public String getalamat_tagihan(Cursor c) {
            return(c.getString(5));
        }

    public String getalamat_tagihan_tt(Cursor c) {
            return(c.getString(6));
        }

    public String getalamatkantor(Cursor c) {
            return(c.getString(7));
    }

    public String getcurenttime(Cursor c) {
        return(c.getString(8));
    }

这是将数据输入数据库的类:

public class FormSpaj extends Activity {
    Cursor model=null;
    spaj1_adapter adapter=null;
    DBSpaj helper=null;

    private String selectedImagePath = "";
    String imgPath  ;

    View popupSet;
    PopupWindow pw; 
    List<model_spaj1> listspaj=new ArrayList<model_spaj1>();
    private static final int CAMERA_REQUEST = 1888; 
    private DrawingView drawView;
    int TAKE_PHOTO_CODE = 0;
    public static int count=0;
    final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
        ListView list;

        Uri outputFileUri,mPhotoUri;
        ContentValues values = new ContentValues();

        String type=null;
        String type_tt=null;
        String jekel=null;
        String jekel_tt=null;

        EditText agama,agamatt,alamat,alamat_tt,alamat_tagihan,alamat_tagihan_tt,alamatkantor,getcurenttime;

        Spinner status,status_tt,jenis_kelamin_tt,jenis_kelamin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
         setContentView(R.layout.coba_form);
         popUp = new PopupWindow(this);
         layout = new LinearLayout(this);

         helper=new DBSpaj(this);
         model=helper.getAll();
         startManagingCursor(model);
//       adapter=new spaj1_adapter(model);

        agama=(EditText)findViewById(R.id.agama);
         agamatt=(EditText)findViewById(R.id.agama_tt);
         alamat=(EditText)findViewById(R.id.alamat);
         alamat_tagihan=(EditText)findViewById(R.id.alamat_tagihan);
         alamat_tagihan_tt=(EditText)findViewById(R.id.alamat_tagihan_tt);
         alamat_tt=(EditText)findViewById(R.id.alamat_tt);
         alamatkantor=(EditText)findViewById(R.id.alamatkantor);
         getcurenttime=(EditText)findViewById(R.id.getcurenttime);

       public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.cobaform, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.submit:
                    Submit();
                    break;
                case R.id.list_spaj:
                    dialogListSpaj();
                    break;
            }
            return true;
        }


        @Override
        public void onDestroy() {
            super.onDestroy();

            helper.close();
        }

        private void Submit() {

            GetTime.getCurrentDate("yyyyMMddhhmmss");
            getcurenttime.setText(GetTime.getCurrentDate("yyyyMMddhhmmss"));

            System.out.println(getcurenttime);


            helper.insert(agama.getText().toString(),
                    agamatt.getText().toString(),
                    alamat.getText().toString(),
                    alamat_tt.getText().toString(),
                    alamat_tagihan.getText().toString(),
                    alamat_tagihan_tt.getText().toString(),
                    alamatkantor.getText().toString(),
                    getcurenttime.getText().toString()
                    );

            model.requery();
            agama.setText(null);
            agamatt.setText(null);
            alamat.setText(null);
            alamat_tt.setText(null);
            alamat_tagihan.setText(null);
            alamat_tagihan_tt.setText(null);
            alamatkantor.setText(null);

        }

        private void dialogListSpaj() {
            LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            popupSet = inflater.inflate(R.layout.list_spaj1save, null);
            closeBtn=(ImageView)popupSet.findViewById(R.id.closeBtn);

            pw = new PopupWindow(popupSet, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
            ListView list = (ListView) popupSet.findViewById(android.R.id.list);
            pw.showAtLocation(findViewById(R.id.coba_form), Gravity.CENTER, 0, 0); 
            adapter= new spaj1_adapter(model);
            list.setAdapter(adapter);
            list.setOnItemClickListener(onListClick);

            closeBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    pw.dismiss();
                }
            });
        }


            private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                pw.dismiss();   
                model.moveToPosition(position);
                agama.setText(helper.getagama(model));
                agamatt.setText(helper.getagamatt(model));
                alamat.setText(helper.getalamat(model));
                alamat_tt.setText(helper.getalamat_tt(model));
                alamat_tagihan.setText(helper.getalamat_tagihan(model));
                alamat_tagihan_tt.setText(helper.getalamat_tagihan_tt(model));
                alamatkantor.setText(helper.getalamatkantor(model));
                getcurenttime.setText(helper.getcurenttime(model));
                Intent in = new Intent(FormSpaj.this, SPajView.class);

                startActivity(in);

                }

            };



        class spaj1_adapter extends CursorAdapter {
            @SuppressWarnings("deprecation")
            spaj1_adapter(Cursor c) {
                super(FormSpaj.this, c);
            }

            @Override
            public void bindView(View row, Context ctxt,
                                                     Cursor c) {
                SpajHolder holder=(SpajHolder)row.getTag();

                holder.populateFrom(c, helper);
            }

            @Override
            public View newView(Context ctxt, Cursor c,
                                                     ViewGroup parent) {
                LayoutInflater inflater=getLayoutInflater();
                View row=inflater.inflate(R.layout.viewerlistspaj1, parent, false);
                SpajHolder holder=new SpajHolder(row);

                row.setTag(holder);

                return(row);
            }


        }

        static class SpajHolder {
            private TextView nama=null;
            private TextView nomor_registrasi=null;

            private View row=null;

            SpajHolder(View row) {
                this.row=row;

                nomor_registrasi=(TextView)row.findViewById(R.id.nomor_registrasi);
                nama=(TextView)row.findViewById(R.id.nama);
//              icon=(ImageView)row.findViewById(R.id.icon);
            }

            void populateFrom(Cursor c, DBSpaj helper) {
                nama.setText(helper.getcurenttime(c));
                nomor_registrasi.setText(helper.getedit_nama_pp(c));
            }
            }startActivityForResult(intent, TAKE_PHOTO_CODE);
            }

这是表列表中显示详细信息的类:

public class SPajView extends Activity {
    Cursor model;
    DBSpaj helper;

    private String OUTPUT_FILE;
      Button but,save,close,draw,set,set_tt;
        boolean click = true;
        private ImageView image_spaj,draw_image,startBtn,pauseRecord_Btn,stopBtn,playBtn,pauseBtn,closeBtn,playback_start,playback_pause,playback_stop;

        private MediaPlayer mediaPlayer;
        private MediaRecorder recorder;
        Uri outputFileUri,mPhotoUri;
        ContentValues values = new ContentValues();

    TextView agama,agamatt,alamat,alamat_tt,alamat_tagihan,alamat_tagihan_tt,alamatkantor,alamatkantor_tt,anak1,
    anak1_tt,anak2,anak2_tt,anak3,anak3_tt,edit_bukti,edit_bukti_tt,edit_bulan,edit_bulan_tt,edit_ibu,edit_ibu_tt,edit_nama_pp,
    edit_nama_tt,edit_nomor,edit_nomor_tt,edit_tahun,edit_tahun_tt,edit_tanggal,edit_tanggal_tt,edit_ttl,edit_ttl_tt,
    edit_warga_negara,edit_warga_negara_tt,email_tt,email,hp,hp_tt,klasifikasi_industri,
    klasifikasi_industri_tt,klasifikasi_pekerjaan,klasifikasi_pekerjaan_tt,kode_telepon,kode_telepon_tt,kode_telepon_kantor_tt,
    kode_telepon_tagihan,kode_telepon_tagihan_tt,kodehp,kodehp_tt,kodepos,kodepos_kantor,kodepos_kantor_tt,kodepos_tagihan,
    kodepos_tagihan_tt,kodepos_tt,kodetelepon_kantor,pendidikan,pendidikan_tt,penghasilan,penghasilan_tt,
    suami,suami_tt,sumber,sumber_dana,sumber_dana_tt,sumber_tt,telepon,telepon_kantor,telepon_kantor_tt,
    telepon_tagihan,telepon_tagihan_tt,telepon_tt,ttl_anak1,ttl_anak1_tt,ttl_anak2,ttl_anak2_tt,ttl_anak3,ttl_anak3_tt,
    ttl_suami,ttl_suami_tt,tujuan,tujuan_tt,getcurenttime;

    Spinner status,status_tt,jenis_kelamin_tt,jenis_kelamin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.spaj1_view);

          helper=new DBSpaj(this);
          model=helper.getAll();
          startManagingCursor(model);
          agama=(TextView)findViewById(R.id.agama);
         agamatt=(TextView)findViewById(R.id.agama_tt);
         alamat=(TextView)findViewById(R.id.alamat);
         alamat_tagihan=(TextView)findViewById(R.id.alamat_tagihan);
         alamat_tagihan_tt=(TextView)findViewById(R.id.alamat_tagihan_tt);
         alamat_tt=(TextView)findViewById(R.id.alamat_tt);
         alamatkantor=(TextView)findViewById(R.id.alamatkantor);
         getcurenttime=(TextView)findViewById(R.id.getcurenttime);


            agama.setText(helper.getagama(model));
            agamatt.setText(helper.getagamatt(model));
            alamat.setText(helper.getalamat(model));
            alamat_tt.setText(helper.getalamat_tt(model));
            alamat_tagihan.setText(helper.getalamat_tagihan(model));
            alamat_tagihan_tt.setText(helper.getalamat_tagihan_tt(model));
            alamatkantor.setText(helper.getalamatkantor(model));

            getcurenttime.setText(helper.getcurenttime(model));

    }

    }

这是我的日志:

09-30 14:45:42.532: E/AndroidRuntime(19033): FATAL EXCEPTION: main
09-30 14:45:42.532: E/AndroidRuntime(19033): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.touch/org.example.touch.SPajView}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 7
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.os.Looper.loop(Looper.java:137)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.ActivityThread.main(ActivityThread.java:4895)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at java.lang.reflect.Method.invokeNative(Native Method)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at java.lang.reflect.Method.invoke(Method.java:511)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at dalvik.system.NativeStart.main(Native Method)
09-30 14:45:42.532: E/AndroidRuntime(19033): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 7
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at org.example.touch.DBSpaj.getagama(DBSpaj.java:320)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at org.example.touch.SPajView.onCreate(SPajView.java:156)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.Activity.performCreate(Activity.java:5163)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-30 14:45:42.532: E/AndroidRuntime(19033):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
09-30 14:45:42.532: E/AndroidRuntime(19033):    ... 11 more

我不知道为什么我不能显示我以前输入的数据,希望有人能帮助我解决我的问题,谢谢。

4

2 回答 2

5

当您的查询返回一个新游标时,它最初指向结果索引 -1,这意味着“在第一行之前”。您必须首先调用其中一种移动方法,例如moveToFirst()在光标上将其移动到有效的结果行。(检查移动结果以确保它指向有效行。)

在您的堆栈跟踪中,您可以看到游标行访问getagama()位于您的SPajView.onCreate(). moveToFirst()因此,要修复此特定崩溃,请在中添加以下内容SPajView.onCreate()

if (model.moveToFirst()) {
    agama.setText(helper.getagama(model));
    //...
}
于 2013-09-30T08:19:49.057 回答
0

你不使用这个:

    agama.setText(helper.getagama(model));
    agamatt.setText(helper.getagamatt(model));
    alamat.setText(helper.getalamat(model));
    alamat_tt.setText(helper.getalamat_tt(model));
    alamat_tagihan.setText(helper.getalamat_tagihan(model));
    alamat_tagihan_tt.setText(helper.getalamat_tagihan_tt(model));
    alamatkantor.setText(helper.getalamatkantor(model));

    getcurenttime.setText(helper.getcurenttime(model));

因为 model是返回光标。你也可以使用 SimpleCursorAdapter

请参阅教程或非常有用。

于 2013-09-30T08:21:02.487 回答