Java Android中的非法状态例外?

Java Android中的非法状态例外?,java,android,android-sqlite,Java,Android,Android Sqlite,我的活动中有微调器和列表视图 我正在从中的SQLite数据库表中检索平均值为的所有列 我的列表视图 我在原始查询中使用了带有GROUPBY子句的average函数 当我运行应用程序时,我的应用程序崩溃 我收到空指针异常: Bad request for field slot 0,-1. numRows = 1, numColumns = 6 and java.lang.IllegalStateException: get field slot from row 0 col -1 failed.

我的活动中有微调器和列表视图

  • 我正在从中的SQLite数据库表中检索平均值为的所有列 我的列表视图

  • 我在原始查询中使用了带有GROUPBY子句的average函数

  • 当我运行应用程序时,我的应用程序崩溃

    我收到空指针异常:

    Bad request for field slot 0,-1. numRows = 1, numColumns = 6 and java.lang.IllegalStateException: get field slot from row 0 col -1 failed.
    
    谁能帮帮我吗。提前谢谢

    这是我的活动代码

    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.performance_details);
            databaseHelper = new DatabaseHelper(this);
            databaseHelper.onOpen(db);
    
            spinneEmployeeName = (Spinner)findViewById(R.id.spinnerPerformance_EmployeeName);
            loadSerachEmpName();
            spinneEmployeeName.setOnItemSelectedListener(new OnItemSelectedListener()
            {
    
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    selectedEmployeeName = spinneEmployeeName.getSelectedItem().toString().trim();
                    System.out.println("selectedEmployeeName " + selectedEmployeeName);
    
                    String[] separated = selectedEmployeeName.split(" ");
                    strSeparated_Id = separated[0].trim();
                    Log.e("strSeparated_Id  = ",""+strSeparated_Id);
                    System.out.println("strSeparated_Id  = " +strSeparated_Id);
    
                    strSeparated_EmpName = separated[1].trim();
                    Log.e("strSeparated_EmpName  = ",""+strSeparated_EmpName);
                    System.out.println("strSeparated_EmpName = " +strSeparated_EmpName);
    
    
                     showPerformanceDetails();
    
    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
    
                }
            });
    
    }
    
        private void showPerformanceDetails()
        {
             list_PerformanceDetails = (ListView)findViewById(R.id.list_PerformanceDetails);
    
            ArrayList<Performance_Pojo> Performance_PojoList = new ArrayList<Performance_Pojo>();  
            Performance_PojoList.clear();   
    
            SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
            Log.e("strSeparated_Id  = ",""+strSeparated_Id);
            Log.d("Tag", strSeparated_Id);
            Cursor cursor = sqlDatabase.rawQuery("SELECT performance_month, AVG(performance_rate_one),  AVG(performance_rate_two),  AVG(performance_rate_three),  AVG(performance_rate_four),  AVG(performance_rate_five)  FROM performance where "+ "Emp_id" + " = ? " 
                    +" GROUP BY performance_month",new String[]{String.valueOf(strSeparated_Id)});
    
            if (cursor != null && cursor.getCount() != 0) 
            { 
                if (cursor.moveToFirst())
                {
                    do
                    {
                        Performance_Pojo Performance_PojoListItems = new Performance_Pojo();  
    
                        Performance_PojoListItems.set_strPerformanceMonth(cursor.getString(cursor.getColumnIndex("performance_month")));
                        Performance_PojoListItems.set_strPerformance_rate_one(cursor.getString(cursor.getColumnIndex("performance_rate_one")));
                        Performance_PojoListItems.set_strPerformance_rate_two(cursor.getString(cursor.getColumnIndex("performance_rate_two")));
                        Performance_PojoListItems.set_strPerformance_rate_three(cursor.getString(cursor.getColumnIndex("performance_rate_three")));
                        Performance_PojoListItems.set_strPerformance_rate_four(cursor.getString(cursor.getColumnIndex("performance_rate_four")));
                        Performance_PojoListItems.set_strPerformance_rate_five(cursor.getString(cursor.getColumnIndex("performance_rate_five")));
    
                        Performance_PojoList.add(Performance_PojoListItems);     
    
                    }while (cursor.moveToNext());   
                }
                sqlDatabase.close();
                cursor.close();
            }
    
    
    
            PerformanceList_Adapter performanceList_Adapter = new PerformanceList_Adapter(Performance_Details.this, Performance_PojoList); 
            list_PerformanceDetails.setAdapter(performanceList_Adapter);
    
    
        }
    
    这是我的日志猫错误信息

    07-01 16:43:31.746: E/CursorWindow(15747): Bad request for field slot 0,-1. numRows = 1, numColumns = 6
    07-01 16:43:31.746: D/AndroidRuntime(15747): Shutting down VM
    07-01 16:43:31.746: W/dalvikvm(15747): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    07-01 16:43:31.766: E/AndroidRuntime(15747): FATAL EXCEPTION: main
    07-01 16:43:31.766: E/AndroidRuntime(15747): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.database.CursorWindow.getString_native(Native Method)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.database.CursorWindow.getString(CursorWindow.java:329)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.sqlitedemo.Performance_Details.showPerformanceDetails(Performance_Details.java:92)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.sqlitedemo.Performance_Details.access$0(Performance_Details.java:70)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.sqlitedemo.Performance_Details$1.onItemSelected(Performance_Details.java:56)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.widget.AdapterView.access$200(AdapterView.java:42)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.os.Handler.handleCallback(Handler.java:587)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.os.Handler.dispatchMessage(Handler.java:92)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.os.Looper.loop(Looper.java:123)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.app.ActivityThread.main(ActivityThread.java:3683)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at java.lang.reflect.Method.invoke(Method.java:507)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at dalvik.system.NativeStart.main(Native Method)
    

    发生这种情况是因为您正在获取值
    AVG(performance\u rate\u one)
    并在搜索名为
    performance\u rate\u one
    的列时。因此getColumnIndex将返回-1,因此您将得到错误

    在查询中使用
    别名
    ,或者在获取时将列名用作
    AVG(performance\u rate\u one)

    编辑

    例如:如果在查询中选择字段作为
    AVG(performance\u rate\u one)作为col1

    然后在获取时,您应该编写
    cursor.g‌​etColumnIndex(“col1”)


    希望这有帮助。

    是否有与您的sqlite查询条件相匹配的记录?我使用别名来列,但仍然得到java.lang.IllegalStateException的相同错误:从第0行获取字段槽col-1失败您使用了什么别名?例如=Cursor Cursor=sqlDatabase.rawQuery(“选择性能\月,平均性能\费率\一)作为col1,平均(performance_rate_two)作为col2,AVG(performance_rate_two)作为col3,AVG(performance_rate_two)作为col4,AVG(performance_rate_two)作为col5,其中“+”Emp_id“+”=?“+”按性能分组“月”,新字符串[]{String.valueOf(stresparated_id)});以及获取部分,请在代码行=Performance_PojoListItems.set_strPerformanceMonth(cursor.getString(cursor.getColumnIndex(“Performance_month”))处获取相同的错误和异常;
      Performance_PojoListItems.set_strPerformance_rate_one(cursor.getString(cursor.getColumnIndex("performance_rate_one")));
    
    07-01 16:43:31.746: E/CursorWindow(15747): Bad request for field slot 0,-1. numRows = 1, numColumns = 6
    07-01 16:43:31.746: D/AndroidRuntime(15747): Shutting down VM
    07-01 16:43:31.746: W/dalvikvm(15747): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    07-01 16:43:31.766: E/AndroidRuntime(15747): FATAL EXCEPTION: main
    07-01 16:43:31.766: E/AndroidRuntime(15747): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.database.CursorWindow.getString_native(Native Method)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.database.CursorWindow.getString(CursorWindow.java:329)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.sqlitedemo.Performance_Details.showPerformanceDetails(Performance_Details.java:92)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.sqlitedemo.Performance_Details.access$0(Performance_Details.java:70)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.sqlitedemo.Performance_Details$1.onItemSelected(Performance_Details.java:56)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.widget.AdapterView.access$200(AdapterView.java:42)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.os.Handler.handleCallback(Handler.java:587)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.os.Handler.dispatchMessage(Handler.java:92)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.os.Looper.loop(Looper.java:123)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at android.app.ActivityThread.main(ActivityThread.java:3683)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at java.lang.reflect.Method.invoke(Method.java:507)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    07-01 16:43:31.766: E/AndroidRuntime(15747):    at dalvik.system.NativeStart.main(Native Method)