Вопрос

I know there is a lot of common questions like this out there but I just can't seem to find the solution. When I attempt to initialize my loader using getLoaderManager().initLoader(LOADER_ID, null, this); The error The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, Gridview) comes up.

This leads me to believe that the program is not recognizing that Gridview implements the loader manager. I'm not sure why this is and where to go from here. I tried playing around with different imports but that didn't work. I also made sure I had the proper downloads to support loaders. The code I am using is below.

package com.example.camerapreview;

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

//Omissis imports


public class Gridview extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{       
    private static final String TAG = "Checking Database";
    private static final String TAG1 = "Checking Thumbnail";

    Cursor cursor;
    int columnindexid;
    int columnindexdata;
    int videoidindex;
    int videopathindex;
    GridviewData entry;
    GridView gridview;
    VideoAdapter videoadapter;
    Cursor curs;

    ImageLoaderConfiguration config;

    String[] mediaColumns = {
        MediaStore.Video.Media._ID,
        MediaStore.Video.Media.DATA,
        MediaStore.Video.Media.TITLE,
        MediaStore.Video.Media.MIME_TYPE
    };

    private static final int LOADER_ID = 1;
    int flags = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview);
        gridview = (GridView) this.findViewById(R.id.gridview);


        cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
        columnindexid = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
        columnindexdata = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);

        entry = new GridviewData(this);
        entry.open();

        getLoaderManager().initLoader(LOADER_ID, null, this);
        DataEntry putitin = new DataEntry(entry, this);
        putitin.execute();

        //the cursor used in the cursor adapater
        curs = entry.adapterCursor();
        videoidindex = entry.Indexfinder(curs);
        videopathindex = entry.Indexfinder2(curs);

        config = new ImageLoaderConfiguration.Builder(this)
                        .imageDownloader(new BaseImageDownloader(this))
                        .build();

        ImageLoader.getInstance().init(config);
        Log.i(TAG, "Before set adapater");
        gridview.setAdapter(new VideoAdapter(this, curs, flags));
     }
}

EDIT:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = { GridviewData.VIDEOID, GridviewData.VIDEOFILEPATH };
    return new CursorLoader(Gridview.this, MyContentProvider.CONTENT_URI, projection, null, null, null);
}

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

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    cursoradapter.swapCursor(null);     
}
Это было полезно?

Решение

The error is caused by your imports:

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

they would be fine for FragmentActivity but you are using a normal Activity so they should be:

import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.widget.CursorAdapter;

Please note that in this case your android:minSdkVersion should be 11. If you need compatibility with lower versions, just keep the imports as they are and use FragmentActivity.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top