GetLoaderManager (). initLoader () does not accept 'this' as an argument, although the (ListFragment) class implements LoaderManager.LoaderCallbacks <Cursor>

I'm having problems using the SQLite tutorial on Android. I use ListFragment instead of ListActivity (as in the example), so LoaderManager.LoaderCallbacks<Cursor> used LoaderManager.LoaderCallbacks<Cursor> . Then in the fillData() method in 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 an error message:

 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, although this implements LoaderManager.LoaderCallbacks<Cursor> .

Thanks for any ideas.

+56
android sqlite android-listfragment android-loadermanager
Jun 22 '12 at 12:00
source share
12 answers

You are not using the correct implementations of CursorLoader and Loader . Remove the old import and use the following:

 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: Since I need to extend SherlockListActivity , the NO getSupportLoadManager() method is missing.

Any ideas on this?

EDIT: Follow this tutorial if you don't know how to use fragments. Create a new class with the SherlockFragment extension and move your display logic. Make your past activities an extended SherlockFragmentActivity and show the newly created SherlockFragment. So I started to work. Thanks @JakeWharton!

+77
Jul 11 '12 at 18:47
source share

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

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

     getSupportLoaderManager().initLoader(LOADER_ID, null, this); 
  • You mentioned this when you said that you are using ListFragment, but it repeats: do not increase activity, otherwise the support package will not work. Instead, extend the FragmentActivity or ListFragment class.

  • For your import, make sure that you are using the correct version if your 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 ...

+52
Jul 06 2018-12-12T00:
source share

Sending the third argument solved the problem in my case:

of

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

to

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

Note:

  • minSdk was 8, and I used the v4 support library.
  • (android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>) this) does not work.
  • getSupportLoaderManager() or getSupportLoadManager() does not work.
  • This code was inside an action, not a fragment
+7
Jun 18 '14 at 6:21
source share

Late, but maybe help someone. if you use the maneger loader in the fragment, and you min api below HONEYCOMB
you shuld use this import

 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 to start the bootloader use this code

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

hope this helps someone.

+3
Jun 20 '16 at 11:47
source share

My mistake was beacause this:

 //import android.app.ListFragment; Error when doesnt import from support.v4 import android.support.v4.app.ListFragment; 
+2
01 Oct '14 at 1:47
source share

In my case, I had to extend the ActionBarActivity from the android.support.v7.app package. Then I was able to use

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

+2
Jun 24 '15 at
source share

Change import 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); 
+2
Feb 12 '16 at 14:51
source share

I am using ActionBarSherlock with my application, and I also ran into this problem and worked through all the steps discussed by others on this issue. However, I continued to experience the same problem, having tried all the proposed resolutions.

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

The problem was that I expected Eclipse to tell me when I missed something, and that told me about it, but not the way I used to. Normally, Eclipse will tell me in other cases that I don’t have enough overrides for something to work, but that doesn’t mean it explicitly. I finally raised a question on "LoaderManager.LoaderCallbacks" and realized that I had no callbacks, so this error was really a very reliable error. Adding basic overrides resolved my problem and allowed me to move forward.

 // 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); } 
+1
Feb 02 '13 at 16:52
source share

I got around this using SherlockListFragment (or you can use ListFragment, I suppose), but kept it inside an Activity. First, I created a generic FragmentHolderActivity class that looks like this:

FragmentHolderActivity.java

 public class FragmentHolderActivity extends SherlockFragmentActivity { public static final String FRAGMENT_LAYOUT_RESOURCE_ID = "fragment_layout_resource_id"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Integer fragmentLayoutResourceId = getIntent().getIntExtra(FRAGMENT_LAYOUT_RESOURCE_ID, Integer.MAX_VALUE); Assert.assertNotSame(fragmentLayoutResourceId, Integer.MAX_VALUE); setContentView(fragmentLayoutResourceId); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return false; default: return super.onOptionsItemSelected(item); } } } 

Then you need to create an XML file for your fragment.

your_list_fragment.xml

 <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.example.YourListFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment" /> 

And here is the code you use to run the fragment:

  Intent intent = new Intent(); intent.setClass(this, FragmentHolderActivity.class); intent.putExtra(FragmentHolderActivity.FRAGMENT_LAYOUT_RESOURCE_ID, R.layout.your_list_fragment); startActivity(intent); 

You just tell FragmentHolderActivity to use your_list_fragment layout, which in turn loads MyListFragment.java

Then you can use: getSherlockActivity().getSupportLoaderManager().initLoader(...) in YourListFragment.java

Not sure if this is the right approach, but it retains my logic in Fragments, which is nice.

0
Jan 21 '13 at 14:38
source share

Get in the same problem. My minSdkVersion is 14, so it cannot use the android.support.v4 package.

I figured this out by extending LoaderCallbacks instead of LoaderManager.LoaderCallbacks and using these packages

 import android.app.LoaderManager.LoaderCallbacks; import android.content.CursorLoader; import android.database.Cursor; import android.widget.SimpleCursorAdapter; 
0
Nov 15 '13 at 1:13
source share

If you tried all of the above methods and still encountered the same error with the "this" parameter, follow these steps:

  • Go to settings and enable import on the fly. (by default it will be and you can do it using Alt + Enter keys).

  • Cut all action code that is implemented
    LoaderCallback ... and paste it into a text editor.

  • Then, finally, copy all the code for this action from the text editor where you pasted, without any import commands. (Only class / activity code).

  • You will have many errors because you have not imported anything yet. Just press Alt + Enter, where you get errors, and these libraries will be imported automatically. Note. Select the android.app ... library for CursorLoaders.

0
Apr 23 '17 at 19:01
source share

I had a similar problem when my AsyncTaskLoader did not return my list, but I decided to change the call from .initLoader to .restartLoader.

So, I actually never called .initLoader and immediately called .restartLoader.

I had an onListItemClick listener in my fragment, and each time it returned from the fragment and reloaded onListItemClick and moved through the application, and it continued to crash.

It may just be specific to my application, but hopefully it helps others if they have problems reloading fragments.

This was part of the file manager part in my application, so it is specific to pushing a few onListItemClicks in the fragment you are loading.

0
Dec 02 '17 at 22:12
source share



All Articles