许多人建议使用LoaderManager和CursorLoader,而不是使用一些不推荐使用的SimpleCursorAdapter构造函数。所以当调用getLoaderManager()。initLoader()时,它会给我这个错误:
LoaderManager类型中的方法initLoader(int,Bundle,LoaderManager.LoaderCallbacks)不适用于参数(int,null,SearchResultsActivity)
我已经尝试导入了loadermanager和cursorloader的v4版本,但这似乎不起作用。我也尝试过getSupportLoaderManager(),它也不能正常工作。我注意到有些人正在收到这个错误并且已经查看了对话以尝试找到解决方案,但我找到的解决方案却无效。我在showResults()方法btw
中调用LoaderManager可搜索活动的代码:
import android.os.Bundle;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.app.SearchManager;
import android.app.ListActivity;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
public class SearchResultsActivity extends FragmentActivity {
private ListView list;
DatabaseTable db;
LoaderManager lm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
db = new DatabaseTable(this);
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
db = new DatabaseTable(this);
Cursor cursor = db.getContactMatches(query, null);
list = (ListView)findViewById(android.R.id.list);
getSupportLoaderManager().initLoader(0, null, this);
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
null, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
list.setAdapter(mAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent contactIntent = new Intent(getApplicationContext(), ContactActivity.class);
contactIntent.setData(getIntent().getData());
startActivity(contactIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
}
答案 0 :(得分:0)
LoaderManager类型中的方法
initLoader(int, Bundle, LoaderManager.LoaderCallbacks)
不适用于参数(int, null, SearchResultsActivity)
简单地说:SearchResultsActivity
必须实施LoaderManager.LoaderCallbacks
。否则this
不会引用LoaderManager.LoaderCallbacks
...