-2
我想簡化我的代碼,對變量在可能降低,並且我一直在logcat中收到此錯誤時,我的應用程序運行,並且應用程序關閉:switch語句不工作
02-14 00:13:10.279 32478-32478/com.example.chris.sunil_gupta W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415998e0)
02-14 00:13:10.289 32478-32478/com.example.chris.sunil_gupta E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chris.sunil_gupta/com.example.chris.sunil_gupta.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500
有人能告訴我我的代碼有什麼問題嗎?此代碼工作完美,但我想我可以簡化一點:
package com.example.chris.sunil_gupta;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;
public class MainActivity extends Activity {
private List <CallData>list = new ArrayList<CallData>();
private Context context=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
ListView listview;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
listview=(ListView)findViewById(R.id.ListView_CallData);
getCallDetails();
CustomAdapter adapter = new CustomAdapter(MainActivity.this, list);
listview.setAdapter(adapter);
}
public void getCallDetails()
{
// cursor1 gets all the items in the calllog and arranges them from newest call down
Cursor cursor1 = getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
//looks like all the cell values in the calllog database are integers
int number = cursor1.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor1.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor1.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor1.getColumnIndex(CallLog.Calls.DURATION);
//declare some new variables here; we're going to convert the integers into these
String callType;
String phoneNumber;
String callDate;
String callDuration;
Date callDateTime;
//go through all the rows in the db and convert the values to strings or whatever
while (cursor1.moveToNext())
{
phoneNumber = cursor1.getString(number);
callType = cursor1.getString(type);
callDate = cursor1.getString(date);
callDateTime = new Date(Long.valueOf(callDate));
callDuration = cursor1.getString(duration);
// the string cType will give us text of either outgoing, incoming or missed
String cType = null;
int cTypeCode = Integer.parseInt(callType);
switch(cTypeCode)
{
case CallLog.Calls.OUTGOING_TYPE:
cType = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
cType= "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
cType = "MISSED";
break;
}
CallData calldata=new CallData(cType, phoneNumber, callDateTime, callDuration);
list.add(calldata);
}
cursor1.close();
}
}
此代碼給我的錯誤。基本上,我已經改變了:
int type = cursor1.getColumnIndex(CallLog.Calls.TYPE);
到
String type = cursor1.getString(cursor1.getColumnIndex(CallLog.Calls.TYPE));
和
取代cTypeCode變量有:
switch (Integer.parseInt(type))
下面是給我的錯誤代碼:
package com.example.chris.sunil_gupta;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;
public class MainActivity extends Activity {
private List <CallData>list = new ArrayList<CallData>();
private Context context=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
ListView listview;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
listview=(ListView)findViewById(R.id.ListView_CallData);
getCallDetails();
CustomAdapter adapter = new CustomAdapter(MainActivity.this, list);
listview.setAdapter(adapter);
}
public void getCallDetails()
{
// cursor1 gets all the items in the calllog and arranges them from newest call down
Cursor cursor1 = getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
//looks like all the cell values in the calllog database are integers
int number = cursor1.getColumnIndex(CallLog.Calls.NUMBER);
// int type = cursor1.getColumnIndex(CallLog.Calls.TYPE);
String type = cursor1.getString(cursor1.getColumnIndex(CallLog.Calls.TYPE));
int date = cursor1.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor1.getColumnIndex(CallLog.Calls.DURATION);
//declare some new variables here; we're going to convert the integers into these
String phoneNumber;
String callDate;
String callDuration;
Date callDateTime;
//go through all the rows in the db and convert the values to strings or whatever
while (cursor1.moveToNext())
{
phoneNumber = cursor1.getString(number);
callDate = cursor1.getString(date);
callDateTime = new Date(Long.valueOf(callDate));
callDuration = cursor1.getString(duration);
// the string cType will give us text of either outgoing, incoming or missed
String cType = null;
switch (Integer.parseInt(type))
{
case CallLog.Calls.OUTGOING_TYPE:
cType = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
cType= "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
cType = "MISSED";
break;
}
CallData calldata=new CallData(cType, phoneNumber, callDateTime, callDuration);
list.add(calldata);
}
cursor1.close();
}
}
感謝Karakuri,我不與Android工作室的時刻。稍後再看,並且如果它有效,肯定標記它是正確的。爲什麼它在第一種情況下工作,而不是第二種情況? 'int type = cursor1.getColumnIndex(CallLog.Calls.TYPE);'也在while循環之外。 – CHarris
@ChristopheHarris因爲在第一種情況下,您只查找列索引,但在第二種情況下,您還嘗試使用'getString()'從該列檢索值。 – Karakuri