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

后端 未结 19 1573
无人共我
无人共我 2020-12-07 16:19

I\'m having trouble following a guide on using SQLite in Android. I\'m using a ListFragment instead of a ListActivity(as in the example), so I have

相关标签:
19条回答
  • 2020-12-07 16:48

    I am using minSDK 27 and had this same issue, I tried to cast like @Dexter suggest, but that gave an error saying cannot be cast to android.app.LoaderManager$LoaderCallbacks, so I then tried to use different import statements and this worked. I commented out the v4 versions and this is what I have now and the app is working:

    //import android.support.v4.app.LoaderManager;
    import android.app.LoaderManager;
    import android.support.v4.app.NavUtils;
    //import android.support.v4.content.CursorLoader;
    import android.content.CursorLoader;
    //import android.support.v4.content.Loader;
    //import android.content.Loader;
    

    Not quite sure when/how the v4 versions were imported.

    0 讨论(0)
  • 2020-12-07 16:51

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

    1. Go to settings and enable imports on the fly option.(by default it'll be enabled and you can do this by using Alt+Enter keys).

    2. Cut the whole code of the activity which had implemented
      LoaderCallback... and paste it in a text editor.

    3. Then at last copy the whole code of that activity from the text editor where you had pasted, without any import commands.( Only the code of the class/activity).

    4. You'll get lots of errors as you have imported nothing yet. Just press Alt+Enter wherever you're getting the errors and it's libraries will be imported automatically. Note : Choose android.app... library for CursorLoaders.

    0 讨论(0)
  • 2020-12-07 16:53

    My error was beacause this:

    //import android.app.ListFragment; Error when doesnt import from support.v4
    import android.support.v4.app.ListFragment;
    
    0 讨论(0)
  • 2020-12-07 16:53

    Ran into the same problem. My minSdkVersion is 14, so cannot use android.support.v4 package.

    I figured it by extending LoaderCallbacks, instead of LoaderManager.LoaderCallbacks and use these packages

    import android.app.LoaderManager.LoaderCallbacks;  
    import android.content.CursorLoader;  
    import android.database.Cursor;  
    import android.widget.SimpleCursorAdapter;
    
    0 讨论(0)
  • 2020-12-07 16:59

    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;
      

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

    0 讨论(0)
  • 2020-12-07 16:59

    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.

    So I actually never called .initLoader and just immediately called .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.

    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.

    0 讨论(0)
提交回复
热议问题