Android 尽管类 (ListFragment) 实现了 LoaderManager.LoaderCallbacks<Cursor>,但 getLoaderManager().initLoader() 不接受“this”作为参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11155753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:11:41  来源:igfitidea点击:

getLoaderManager().initLoader() doesn't accept 'this' as argument though the class (ListFragment) implements LoaderManager.LoaderCallbacks<Cursor>

androidsqliteandroid-loadermanagerandroid-listfragment

提问by vurp0

I'm having trouble following a guideon using SQLite in Android. I'm using a ListFragmentinstead of a ListActivity(as in the example), so I have the ListFragmentimplement LoaderManager.LoaderCallbacks<Cursor>instead. Then, in the fillData()method in the ListFragment:

我在遵循有关在 Android 中使用 SQLite的指南时遇到问题。我使用的是 aListFragment而不是 a ListActivity(如示例中所示),所以我使用了ListFragment工具LoaderManager.LoaderCallbacks<Cursor>。然后,在fillData()方法中ListFragment

private void fillData() {
    // Fields from the database (projection)
    // Must include the _id column for the adapter to work
    String[] from = new String[] { NotesSQLiteHelper.COLUMN_TITLE };
    // Fields on the UI to which we map
    int[] to = new int[] { R.id.label };

    getLoaderManager().initLoader(0, null, this); //error
    adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.notes_row, null, from, to, 0);
    setListAdapter(adapter);
}

I get the error:

我收到错误:

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, NotesActivity.ArrayListFragment)

on the marked line even though thisimplements LoaderManager.LoaderCallbacks<Cursor>.

在标记线上,即使this实现LoaderManager.LoaderCallbacks<Cursor>.

Thank you for any ideas.

谢谢你的任何想法。

回答by Matthias B

You are not using the right implementations of CursorLoaderand Loader. Remove your old imports and use these ones:

您没有使用权的实现 CursorLoaderLoader。删除旧的导入并使用这些:

import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;

But I have the same Problem using SherlockActionBar: As I have to extend SherlockListActivitythere is NO method getSupportLoadManager().

但是我在使用 SherlockActionBar 时遇到了同样的问题:因为我必须扩展SherlockListActivity没有方法getSupportLoadManager()

Any ideas on this?

对此有何想法?

EDIT: follow this tutorialif you do not know how to use fragments. Create a new Class with extends SherlockFragment and move your display logic there. Make your old activity extend SherlockFragmentActivity and show the newly created SherlockFragment. This way I got it working. Thanks to @JakeWharton!

编辑:如果您不知道如何使用片段,请按照本教程进行操作。创建一个带有 extends SherlockFragment 的新类并将您的显示逻辑移到那里。使您的旧活动扩展 SherlockFragmentActivity 并显示新创建的 SherlockFragment。这样我就让它工作了。感谢@JakeWharton!

回答by wileyCoyote

A few things to watch out for (from my recent experience battling with this):

需要注意的一些事情(根据我最近与此作斗争的经验):

  1. If your minSDK is set to less than 11 (i.e. level 10 for Gingerbread) and you are using the Support Pack for backward compatibility, make sure you use

    getSupportLoaderManager().initLoader(LOADER_ID, null, this);
    
  2. You mentioned this when you said you are using ListFragment, but it bears repeating: Do not extend Activity, otherwise the support package will not work. Instead, extend the FragmentActivity class or the ListFragment class.

  3. For your imports, make sure you are using the correct versions if your minSDK < 11:

     android.support.v4.app.FragmentActivity;
     android.support.v4.app.LoaderManager;
     android.support.v4.content.Loader;
    
  1. 如果您的 minSDK 设置为小于 11(即 Gingerbread 的级别 10)并且您正在使用支持包以实现向后兼容性,请确保您使用

    getSupportLoaderManager().initLoader(LOADER_ID, null, this);
    
  2. 您在说您使用ListFragment 时提到了这一点,但值得重复:不要扩展Activity,否则支持包将无法工作。相反,扩展 FragmentActivity 类或 ListFragment 类。

  3. 对于您的导入,如果您的 minSDK < 11,请确保您使用正确的版本:

     android.support.v4.app.FragmentActivity;
     android.support.v4.app.LoaderManager;
     android.support.v4.content.Loader;
    

Hope this helps you... or at least someone else...

希望这可以帮助您……或者至少是其他人……

回答by Dexter

Casting the third argument solved the problem in my case:

在我的情况下,铸造第三个参数解决了这个问题:

from

 getLoaderManager().initLoader(0, null, this);

to

 getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks<Cursor>) this);

Note:

笔记:

  1. minSdk was 8 and i was using support library v4.
  2. (android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>) this)did not work.
  3. getSupportLoaderManager() or getSupportLoadManager()did not work.
  4. This code was inside activity not fragment
  1. minSdk 是 8,我使用的是支持库 v4。
  2. (android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>) this)不工作。
  3. getSupportLoaderManager() or getSupportLoadManager()不工作。
  4. 此代码在活动中而不是片段

回答by anandbibek

Change your imports to

将您的导入更改为

import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;

and use

并使用

getSupportLoaderManager().initLoader(0, null, this);

回答by max

It's late but maybe help some one.
if you use loader maneger in fragment and you min api is below HONEYCOMB
you shuld use this imports

已经晚了,但也许可以帮助某人。
如果您在片段中使用加载程序管理器并且您的最小 api 低于 HONEYCOMB,则
您应该使用此导入

import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;      
import android.support.v4.app.Fragment;

and for initiate loader use this code

并为启动加载器使用此代码

getActivity().getSupportLoaderManager().initLoader(/loader stuff*/);

hope this help some one.

希望这对某人有所帮助。

回答by Renan Franca

My error was beacause this:

我的错误是因为:

//import android.app.ListFragment; Error when doesnt import from support.v4
import android.support.v4.app.ListFragment;

回答by Oladipo Olasemo

In my case I had to have my Activity extend ActionBarActivity from the android.support.v7.app package. I was then able to use

就我而言,我必须让我的 Activity 从 android.support.v7.app 包中扩展 ActionBarActivity。然后我就可以使用

getSupportLoaderManager().initLoader(0, null, this);

getSupportLoaderManager().initLoader(0, null, this);

回答by brentil

I'm using ActionBarSherlock with my app and I as well was running into this issue and worked through all the steps discussed by others in this question. However I was continuing to have the same problem after trying all the suggested resolutions.

我在我的应用程序中使用 ActionBarSherlock,我也遇到了这个问题,并完成了其他人在这个问题中讨论的所有步骤。但是,在尝试了所有建议的解决方案后,我仍然遇到同样的问题。

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, this)

The issue was I had fallen into expecting Eclipse to tell me when I was missing something and it was telling me that but not in the way I was used to. Typically Eclipse in other cases would tell me I'm missing the overrides to make something work but it's not directly saying that here. I finally picked up on the "LoaderManager.LoaderCallbacks" being the issue and realized I had no callbacks for it thus this error was actually a very valid error. Adding the basic overrides resolved my issue and allowed me to move forward.

问题是我已经开始期待 Eclipse 在我丢失某些东西时告诉我,它告诉我,但不是以我习惯的方式。通常在其他情况下 Eclipse 会告诉我我缺少使某些工作起作用的覆盖,但在这里并没有直接说。我终于发现“LoaderManager.LoaderCallbacks”是问题所在,并意识到我没有回调,因此这个错误实际上是一个非常有效的错误。添加基本​​覆盖解决了我的问题并允许我继续前进。

// Creates a new loader after the initLoader () call
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  // do work
  return null;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  adapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
  // data is not available anymore, delete reference
  adapter.swapCursor(null);
}

回答by Zachariah Rabatah

I was having a similar issue where my AsyncTaskLoader was not returning my List, but my resolution was to change the call from .initLoader to .restartLoader.

我有一个类似的问题,我的 AsyncTaskLoader 没有返回我的列表,但我的解决方案是将调用从 .initLoader 更改为 .restartLoader。

So I actually nevercalled .initLoader and just immediately called .restartLoader.

所以我实际上从未调用过 .initLoader 而只是立即调用了 .restartLoader。

I had a onListItemClick listener in my fragment and every time backed out of the fragment and reloaded the onListItemClick and navigated through the app it kept crashing.

我的片段中有一个 onListItemClick 侦听器,每次退出片段并重新加载 onListItemClick 并浏览它一直崩溃的应用程序。

It might just be specific to my app but hopefully it helps others if they are having fragment backstack reload issues.

它可能只是特定于我的应用程序,但希望它可以帮助其他人,如果他们遇到片段后端堆栈重新加载问题。

This was part of a file manager portion in my app so it is specific to clicking multiple onListItemClicks in the fragment you are loading.

这是我的应用程序中文件管理器部分的一部分,因此它特定于单击您正在加载的片段中的多个 onListItemClicks。

回答by tarun mittal

Try these 2 lines it will work

试试这 2 行它会起作用

android.support.v4.app.LoaderManager loaderManager = getSupportLoaderManager();

loaderManager.initLoader(LOADER_ID, null,  this);