索引1处的绑定值为空

时间:2013-02-15 21:51:44

标签: java android android-cursoradapter

在我的应用程序中,我有一个列表,显示每位患者的医疗处理记录,列表显示他们的姓名,治疗日期。我希望能够点击特定条目并将其转到编辑屏幕。

应用程序正确添加新条目,它正确列出条目但是当我从列表中选择一个条目时,我得到无法启动活动而消息是索引1处的绑定值为空

我的代码如下

构建我选择的列表

public void fillMedicalList(String editPatientToGet){
    //sql Query
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(Medical.MEDICAL_TABLE_NAME );

    // build the return columns
    String asColumnsToReturn[]= {
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_PATIENTID,
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_PATIENT_GROUP,
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_TREATMENT_DATE,
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_TREATMENT,
            Medical.MEDICAL_TABLE_NAME + "." + Medical._ID}; 
    //end of build columns to return

    //build the where clause - find entries based on patient id
    String valueToFind[] = {editPatientToGet};

    //build the cursor
    mCursor = queryBuilder.query(mDB, asColumnsToReturn, Medical.MEDICAL_PATIENTID + "=?", valueToFind,
            null, null, null);

    //manage the cursor
    startManagingCursor(mCursor);

    //use an adapter to bind the data to the listview
    MedicalListAdapter adapter = new MedicalListAdapter(this, mCursor);
    ListView av = (ListView) findViewById(R.id.medical_list);
    av.setAdapter(adapter);

现在我开始聆听列表中项目的点击

    av.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            final long editMedicalid = id;
            String WhatRecordIsIt = String.valueOf(editMedicalid);

            Log.i("who is it", WhatRecordIsIt);
            Log.d(DEBUG_TAG,WhatRecordIsIt);

            editMedical(editMedicalid);

        } //end of onClick for an item in the list
    }); //end of OnItemClick Listener
} //end of fill PATIENTItem list

此时我的Log.i信息显示它具有正确的记录ID。

现在我的editMedical(editMedicalid)代码如下

    //we've clicked on a patient in the List list and now we're editing it
public void editMedical(Long id){
    String astrArgs[] = {id.toString()};

    String EditColumnsToReturn[]={
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_PATIENTID,
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_PATIENT_GROUP,
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_TREATMENT_DATE,
            Medical.MEDICAL_TABLE_NAME + "." + Medical.MEDICAL_TREATMENT,
            Medical.MEDICAL_TABLE_NAME + "." + Medical._ID}; 

    Cursor c = mDB.query(Medical.MEDICAL_TABLE_NAME, EditColumnsToReturn,
            Medical._ID + "=?", astrArgs, null, null, null, null);

    c.moveToFirst();        
    final String strMedicalName =
        c.getString(c.getColumnIndex(Medical.MEDICAL_PATIENTID));
    Log.i("Patient's Name Is", strMedicalName);
    Log.d(DEBUG_TAG,strMedicalName);

    final String strMedicalType =
        c.getString(c.getColumnIndex(Medical.MEDICAL_PATIENT_GROUP));
    Log.i("what group", strMedicalType);
    Log.d(DEBUG_TAG,strMedicalType);

    final String strMedicalTreatmentDate =
        c.getString(c.getColumnIndex(Medical.MEDICAL_TREATMENT_DATE));
    Log.i("date", strMedicalTreatmentDate);
    Log.d(DEBUG_TAG,strMedicalTreatmentDate);

    final String strMedicalTreatment =
        c.getString(c.getColumnIndex(Medical.MEDICAL_TREATMENT));

    Log.i("treatment was", strMedicalTreatment);
    Log.d(DEBUG_TAG,strMedicalTreatment);

    final Long MedicalId =
        c.getLong(c.getColumnIndex(Medical._ID));

        String CurrentPatient = String.valueOf(MedicalId);

       Log.i("Patient ID is", CurrentPatient);

   Intent editMedical = new Intent(MedicalListActivity.this,
           EditMedicalActivity.class);

   editMedical.putExtra("patientName", strMedicalName);
   editMedical.putExtra("patientGroup", strMedicalType);
   editMedical.putExtra("treatmentDate", strMedicalTreatmentDate);
   editMedical.putExtra("medicalTreatment", strMedicalTreatment);
   String strCurrentPatient = String.valueOf(MedicalId);
   editMedical.putExtra("medicalId", strCurrentPatient);
   MedicalListActivity.this.startActivity(editMedical);

} //End of getting data needed to edit the record

我的log.i显示正在收集的正确数据,但此时我收到错误,因为索引1处的绑定值为空,因此无法启动我的EditMedicalActivity

我已阅读并重读我的代码,无法找到我的错误。

logcat的

02-15 15:33:35.769: E/AndroidRuntime(20339): FATAL EXCEPTION: main
02-15 15:33:35.769: E/AndroidRuntime(20339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.agmsi.medical/com.agmsi.medical.EditMedicalActivity}: java.lang.IllegalArgumentException: the bind value at index 1 is null
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.os.Looper.loop(Looper.java:130)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at java.lang.reflect.Method.invokeNative(Native Method)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at java.lang.reflect.Method.invoke(Method.java:507)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at dalvik.system.NativeStart.main(Native Method)
02-15 15:33:35.769: E/AndroidRuntime(20339): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:237)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:185)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at com.agmsi.medical.MedicalListActivity.fillMedicalList(MedicalListActivity.java:85)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at com.agmsi.medical.MedicalListActivity.onCreate(MedicalListActivity.java:42)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at com.agmsi.medical.EditMedicalActivity.onCreate(EditMedicalActivity.java:41)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-15 15:33:35.769: E/AndroidRuntime(20339):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-15 15:33:35.769: E/AndroidRuntime(20339):    ... 11 more

好了,因为错误发生在本节之后,我猜这是部分

 Intent editMedical = new Intent(MedicalListActivity.this,
       EditMedicalActivity.class);

 editMedical.putExtra("patientName", strMedicalName);
 editMedical.putExtra("patientGroup", strMedicalType);
 editMedical.putExtra("treatmentDate", strMedicalTreatmentDate);
 editMedical.putExtra("medicalTreatment", strMedicalTreatment);
 String strCurrentPatient = String.valueOf(MedicalId);
 editMedical.putExtra("medicalId", strCurrentPatient);
 MedicalListActivity.this.startActivity(editMedical);

在此之后错误显示 - 所以我不确定这是否是这里或其他地方的东西,这就是为什么我发布了涉及的两个领域的代码

3 个答案:

答案 0 :(得分:1)

堆栈跟踪使得看起来像以下行是问题:

Cursor c = mDB.query(Medical.MEDICAL_TABLE_NAME, EditColumnsToReturn,
        Medical._ID + "=?", astrArgs, null, null, null, null);

您是否正在扩展代码库?你确实使用不同的风格。 也许其他风格适合你:

Cursor c = queryBuilder.query(mDB, EditColumnsToReturn, Medical._ID + "=?", astrArgs,
        null, null, null);

答案 1 :(得分:1)

这是一个sqlite(数据库错误)。 查看完代码并将其与我自己的代码进行比较后,您传递给editPatientToGet的{​​{1}}似乎为空

您的logcat可以在此行确认:

fillMedicalList(String editPatientToGet);

答案 2 :(得分:0)

我遇到了同样的问题,因为我将null arg传递给Dao.queryForId(arg)