我在aAllSongsFragment中有一个片段叫做ViewPager。父Activity扩展android.support.v4.app.FragmentActivity
我正在尝试实现LoaderManagerCursorLoader以异步获取游标。下面是AllSongsFragment

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

public class AllSongsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public void onActivityCreated(Bundle savedInstanceState) {
    .
    .
    getLoaderManager().initLoader(0, null, this);
}


@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    Toast.makeText(getActivity(), "YAYYYY", Toast.LENGTH_SHORT);
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] projection = new String[] {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST};
    CursorLoader cursorLoader = new CursorLoader(getActivity(), uri, projection, null, null, null);
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    Toast.makeText(getActivity(), "finished", Toast.LENGTH_SHORT);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    // TODO
}
}

我的问题是oncreateloader()或任何loadermanager回调方法都没有被调用。应用程序也不会崩溃,也没有例外。我做错什么了?

最佳答案

您正在使用支持库类LoaderManagerCursorLoader。因此,您需要使用对supportLoaderManager的调用。
替换

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

具有
getActivity().getSupportLoaderManager().initLoader(0, null, (LoaderCallbacks<Cursor>)this);

10-08 09:01