简体   繁体   English

不能在 getLoaderManager().initLoader() 方法中使用“this”作为参数

[英]Can't use "this" as an argument in the getLoaderManager().initLoader() method

Instead of using some of the deprecated SimpleCursorAdapter constructors, many people have suggested to use LoaderManager and CursorLoader.许多人建议使用 LoaderManager 和 CursorLoader,而不是使用一些已弃用的 SimpleCursorAdapter 构造函数。 So when calling getLoaderManager().initLoader() it gives me this error:所以当调用 getLoaderManager().initLoader() 时它给了我这个错误:

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, SearchResultsActivity) LoaderManager 类型中的方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks) 不适用于 arguments (int, null, SearchResultsActivity)

I have tried importing the v4 version of the loadermanager and cursorloader, but that has not seem to work.我已经尝试导入 v4 版本的 loadermanager 和 cursorloader,但这似乎不起作用。 I have also tried the getSupportLoaderManager(), which doesn't work as well.我还尝试了 getSupportLoaderManager(),但效果不佳。 I noticed that some people are getting this error and have looked through the conversation to try and find solutions, but the ones I have found don't work.我注意到有些人遇到了这个错误并查看了对话以尝试找到解决方案,但我发现的那些不起作用。 I am calling the LoaderManager within the showResults() method btw我在 showResults() 方法中调用 LoaderManager 顺便说一句

Code for searchable activity:可搜索活动的代码:

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;
        }
    }

} }

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, SearchResultsActivity) initLoader(int, Bundle, LoaderManager.LoaderCallbacks)类型的方法initLoader(int, Bundle, LoaderManager.LoaderCallbacks)不适用于参数(int, null, SearchResultsActivity)

Simply put: SearchResultsActivity must implement LoaderManager.LoaderCallbacks . 简而言之: SearchResultsActivity 必须实现LoaderManager.LoaderCallbacks Otherwise this doesn't refer to an instance of LoaderManager.LoaderCallbacks ... 否则, this不会引用LoaderManager.LoaderCallbacks的实例。

First you must implement your class with LoaderManager.LoaderCallbacks and then you can use "this" keyword in initLoader method (remember that support library is deprecated and you use LoaderManager.getInstance(this) instead of getLoaderManager()首先,您必须使用 LoaderManager.LoaderCallbacks 实现 class,然后您可以在 initLoader 方法中使用“this”关键字(请记住支持库已弃用,您使用LoaderManager.getInstance(this)而不是getLoaderManager()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么getLoaderManager.initLoader()第三个参数=这是无效的? - Why getLoaderManager.initLoader() third argument = this IS INVALID? getLoaderManager()。initLoader()虽然没有支持库的类(ListFragment),但不接受&#39;this&#39;作为参数 - getLoaderManager().initLoader() doesn't accept 'this' as argument though the class (ListFragment) without support libraries 尽管类 (ListFragment) 实现了 LoaderManager.LoaderCallbacks,但 getLoaderManager().initLoader() 不接受“this”作为参数<Cursor> - getLoaderManager().initLoader() doesn't accept 'this' as argument though the class (ListFragment) implements LoaderManager.LoaderCallbacks<Cursor> 在Android的API级别10下无法解析getLoaderManager.initLoader() - getLoaderManager.initLoader() can't be resolved under API level 10 in Android 此回调中的 getLoaderManager().initLoader 错误 - getLoaderManager().initLoader error in this callback getLoaderManager()。initLoader(0,null,this); //错误“ this”被加红线 - getLoaderManager().initLoader(0, null, this);//error “this” is redlined getLoaderManager()的args参数的用途.initLoader(...,...,...)? - Purpose of args parameter of getLoaderManager().initLoader(…,…,…)? LoaderManager尚未初始化... getLoaderManager()。initLoader出现问题 - LoaderManager not initializing… a issue with getLoaderManager().initLoader 用 MutableLivedata 替换 getLoaderManager().initLoader<Cursor> - Replace getLoaderManager().initLoader with MutableLivedata<Cursor> getLoaderManager.initLoader出现刺激性错误 - Got irritating error with getLoaderManager.initLoader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM