LoaderManager没有初始化... getLoaderManager().initLoader的问题

Pap*_*000 1 android loader android-loadermanager

我知道有很多这样的常见问题,但我似乎无法找到解决方案.当我尝试使用getLoaderManager().initLoader(LOADER_ID, null, this);错误初始化我的加载器时,LoaderManager类型中的方法initLoader(int,Bundle,LoaderManager.LoaderCallbacks)不适用于参数(int,null,Gridview).

这让我相信程序没有意识到Gridview实现了加载器管理器.我不知道为什么会这样,从哪里开始.我尝试过使用不同的进口,但这不起作用.我还确保我有适当的下载来支持加载器.我正在使用的代码如下.

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));
     }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

@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);     
}
Run Code Online (Sandbox Code Playgroud)

rci*_*ati 11

该错误是由您的导入引起的:

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;
Run Code Online (Sandbox Code Playgroud)

他们会好的,FragmentActivity但你使用正常,Activity所以他们应该:

import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.widget.CursorAdapter;
Run Code Online (Sandbox Code Playgroud)

请注意,在这种情况下,您android:minSdkVersion应该是11.如果您需要与较低版本兼容,请保持导入原样并使用FragmentActivity.