在Android中,从第0行col -1获取字段槽失败

时间:2013-12-20 08:45:27

标签: java android android-sqlite

我想在list-view中检索特定id的所有数据。当我运行应用程序时,我得到了:

  

IllegalStateException:从第0行第1列获取字段槽失败

这是我的活动代码。

dbAdapter=new DataBase_Adapter(New_Lead_Detail_Activity.this).open();
        dataBase = dbAdapter.getDatabaseInstance();

        String strSql = "SELECT name FROM " + DataBase_Adapter.TABLE_NEW_LEAD + " where id = '" +newleadId + "'";
        System.out.println("sql =" + strSql);

        cursor = dataBase.rawQuery(strSql , null);
        System.out.println("Cursor" + cursor);

        if (cursor.getCount() == 1 )
         {
             cursor.moveToFirst();

             actions = new ArrayList<User_Action>();

             String strOrgName=(cursor.getString(cursor.getColumnIndex(DataBase_Adapter.KEY_ORGANIZATION_NAME)));
             if(strOrgName != null)
             {
                 actions.add(new User_Action("Orgenization Name", strOrgName));
                 System.out.println("Orgenization Name = " + strOrgName);
             }


             String strName = (cursor.getString(cursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_NAME)));
             if(strName != null)
             {
                 actions.add(new User_Action("Name", strName));
                 System.out.println("strName =" + strName);
             }

             String strEmail=(cursor.getString(cursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_EMAIL)));
             if(strEmail != null)
             {
                 actions.add(new User_Action("Email ID", strEmail));
                 System.out.println("Email ID =" + strEmail);
             }

             String strMobile=(cursor.getString(cursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_MOBILE)));
             if(strMobile != null)
             {
                 actions.add(new User_Action("Mobile", strMobile));
                 System.out.println("Mobile =" + strMobile);
             }

             String strProduct=(cursor.getString(cursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_Product)));
             if(strProduct != null)
             {
                 actions.add(new User_Action("Product", strProduct));
                 System.out.println("Product =" + strProduct);
             }

             String strBudget=(cursor.getString(cursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_BUDGET)));
             if(strBudget != null)
             {
                 actions.add(new User_Action("Budget", strBudget));
                 System.out.println("Budget =" + strBudget);
             }

adapter = new Action_Adapter();
            setListAdapter(adapter);

     }

}

class Action_Adapter extends ArrayAdapter<User_Action>
{

    Action_Adapter() {
        super(New_Lead_Detail_Activity.this, R.layout.new_lead_detail_item, actions);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

    {
        User_Action action = actions.get(position);
        LayoutInflater inflater = getLayoutInflater();

        View view = inflater.inflate(R.layout.new_lead_detail_item, parent, false);

        TextView label = (TextView) view.findViewById(R.id.label);
        label.setText(action.getLabel());

        TextView data = (TextView) view.findViewById(R.id.data);
        data.setText(action.getData());

        return view;
    }

 }

}

这是log cat info。

12-20 14:05:07.519: E/CursorWindow(9252): Bad request for field slot 0,-1. numRows = 1, numColumns = 1
12-20 14:05:07.559: E/AndroidRuntime(9252): FATAL EXCEPTION: main
12-20 14:05:07.559: E/AndroidRuntime(9252): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lead_management_project/com.lead_management_project.New_Lead_Detail_Activity}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.os.Looper.loop(Looper.java:123)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at java.lang.reflect.Method.invoke(Method.java:507)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at dalvik.system.NativeStart.main(Native Method)
12-20 14:05:07.559: E/AndroidRuntime(9252): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.database.CursorWindow.getString_native(Native Method)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.database.CursorWindow.getString(CursorWindow.java:329)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at com.lead_management_project.New_Lead_Detail_Activity.onCreate(New_Lead_Detail_Activity.java:69)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-20 14:05:07.559: E/AndroidRuntime(9252):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-20 14:05:07.559: E/AndroidRuntime(9252):     ... 11 more

1 个答案:

答案 0 :(得分:2)

需要更改查询

使用以下

"SELECT * FROM " + DataBase_Adapter.TABLE_NEW_LEAD + " where id = '" +newleadId + "'";

而不是

"SELECT name FROM " + DataBase_Adapter.TABLE_NEW_LEAD + " where id = '" +newleadId + "'";