我的活动中有微调器和列表视图。
我正在从我的SQLite数据库表中检索所有列的平均值 我的列表视图。
我使用group by子句在原始查询中使用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);
}
这是我的适配器类
public class PerformanceList_Adapter extends BaseAdapter
{
Context context;
ArrayList<Performance_Pojo> Performance_List;
public PerformanceList_Adapter(Context context,
ArrayList<Performance_Pojo> performance_List) {
super();
this.context = context;
Performance_List = performance_List;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Performance_List.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return Performance_List.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Performance_Pojo PerformanceListItems =Performance_List.get(position);
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.performance_list_items, null);
}
TextView textView_Month = (TextView) convertView.findViewById(R.id.textView_PerformanceMonth);
textView_Month.setText(PerformanceListItems.get_strPerformanceMonth());
TextView textView_RateOne = (TextView) convertView.findViewById(R.id.textView_performance_rate_one);
textView_RateOne.setText(PerformanceListItems.get_strPerformance_rate_one());
TextView textView_RateTwo = (TextView) convertView.findViewById(R.id.textView_performance_rate_two);
textView_RateTwo.setText(PerformanceListItems.get_strPerformance_rate_two());
TextView textView_RateThree = (TextView) convertView.findViewById(R.id.textView_performance_rate_three);
textView_RateThree.setText(PerformanceListItems.get_strPerformance_rate_three());
TextView textView_RateFour = (TextView) convertView.findViewById(R.id.textView_performance_rate_four);
textView_RateFour.setText(PerformanceListItems.get_strPerformance_rate_four());
TextView textView_RateFive = (TextView) convertView.findViewById(R.id.textView_performance_rate_five);
textView_RateFive.setText(PerformanceListItems.get_strPerformance_rate_five());
return convertView;
}
}
行的错误和异常
Performance_PojoListItems.set_strPerformance_rate_one(cursor.getString(cursor.getColumnIndex("performance_rate_one")));
这是我的Log Cat错误信息
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)
答案 0 :(得分:1)
这是发生的事情,因为您正在获取值AVG(performance_rate_one)
并在搜索名称为performance_rate_one
的列时。所以getColumnIndex将返回-1,因此会出现错误。
在查询中使用alias
或在提取时将列名称用作AVG(performance_rate_one)
。
修改强>
例如:如果在“查询”中,则选择字段为AVG(performance_rate_one) as col1
然后在抓取时你应该写cursor.getColumnIndex("col1")
希望这有帮助。