我正在尝试创建一个应用程序,以根据呼叫类型(来电,去电或未接来电)显示呼叫日志。另外,我正在尝试添加搜索和删除功能,以便用户可以按号码搜索呼叫并删除呼叫。该应用的布局如下所示:
如果我按下“全部”按钮以显示所有呼叫并在那里按号码搜索,则搜索功能将起作用,但是在转到其他部分以搜索号码(例如已接或未接)时,应用程序崩溃。
到目前为止,我已经设法在Android Studio上运行logcat,发现主要问题在我的主要活动文件中。我在这里附上了logcat图像:
这些代码段似乎存在问题:我的getCalls方法,afterTextChanged方法和我的updateCursor方法。
private Cursor getCalls(int type, String searchString) {
Uri callUri = CallLog.Calls.CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
String[] projection = new String[]{Calls._ID, Calls.NUMBER, Calls.DURATION, Calls.TYPE};
String selection=null;
String[] selectionArgs=null;
if(type != 0){
// filter type calls
selection = Calls.TYPE + "=?";
selectionArgs = new String[]{String.valueOf(type)};
}
if(!TextUtils.isEmpty(searchString)) {
// has some search string
if(TextUtils.isEmpty(selection)) {
// all call types
selection = Calls.NUMBER + " LIKE ?";
selectionArgs = new String[]{"%"+searchString+"%"};
} else {
// some type of call and add search String
selection = selection+" && " + Calls.NUMBER+" LIKE ?";
selectionArgs = new String[]{selectionArgs[0],"'%"+searchString+"%'"};
}
}
String order = Calls.DATE + " DESC ";
//verify permissions to access the user's call log
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
cursor = contentResolver.query(callUri, // URI content provider
projection,
selection,
selectionArgs,
order);
}
return cursor;
}
@Override
public void afterTextChanged(Editable s) {
updateCursor();
}
//updates the search
void updateCursor() {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
cursor = null;
}
cursor = getCalls(currentCallType, searchET.getText().toString());
adapter.swapCursor(cursor);
}
运行该应用程序后,我希望能够去不同的部分(例如,我去接听电话的部分)并搜索一个号码,但是在转到该部分并点击搜索栏时,应用程序崩溃。我不明白这些方法怎么会出现问题。
答案 0 :(得分:0)
SQLException表示错误在&符号附近,因此错误应在以下行中:
selection = selection+" && " + Calls.NUMBER+" LIKE ?";
用这个替换它,因为在SQL中应该使用'AND'而不是double&:
selection = selection+" AND " + Calls.NUMBER+" LIKE ?";