android LoaderManager中的方法冲突

假设我有一个带有Feed类的Android应用程序,由一些Fragment调用如下实现并抛出: java.lang.ClassCastException: com.newsfeeder.ui.MainFragment cannot be cast to android.app.LoaderManager$LoaderCallbacks

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

public class MainFragment extends Fragment implements   LoaderManager.LoaderCallbacks<List<Feed>>
{   
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View v=inflater.inflate(R.layout.main_container, null);
        listView = (ListView) v.findViewById(R.id.list);
        getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks<Object>) this);

        return v;
    } 
public Loader<List<Feed>> onCreateLoader(int id, Bundle args) {
        final List<Feed> initialItems = items;
        return new ThrowableLoader<List<Feed>>(getActivity(), items) {

            @Override
            public List<Feed> loadData() throws Exception {
                try {
                    if(getActivity() != null) {
                        return serviceProvider.getFeeds(); //some method fetching some `feeds`
                    } else {
                        return Collections.emptyList();
                    }

                } catch (OperationCanceledException e) {
                    Activity activity = getActivity();
                    if (activity != null)
                        activity.finish();
                    return initialItems;
                }
            }
        };
    }

protected List<Feed> items = Collections.emptyList();

Btw ThrowableLoader类实现如下

import android.content.Context;
    public abstract class ThrowableLoader<D> extends AsyncLoader<D> {


        private final D data;

        private Exception exception;

        /**
         * Create loader for context and seeded with initial data
         *
         * @param context
         * @param data
         */
        public ThrowableLoader(Context context, D data) {
            super(context);

            this.data = data;
        }

        @Override
        public D loadInBackground() {
            exception = null;
            try {
                return loadData();
            } catch (Exception e) {
                Ln.d(e, "Exception loading data");
                exception = e;
                return data;
            }
        }

        /**
         * @return exception
         */
        public Exception getException() {
            return exception;
        }

        /**
         * Clear the stored exception and return it
         *
         * @return exception
         */
        public Exception clearException() {
            final Exception throwable = exception;
            exception = null;
            return throwable;
        }

        /**
         * Load data
         *
         * @return data
         * @throws Exception
         */
        public abstract D loadData() throws Exception;
    }

and here's the `AsyncLoader`


import android.content.Context;
import android.support.v4.content.AsyncTaskLoader; 
public abstract class AsyncLoader<D> extends AsyncTaskLoader<D> {
    private D data;

    /**
     * Create async loader
     *
     * @param context
     */
    public AsyncLoader(Context context) {
        super(context);
    }

    @Override
    public void deliverResult(D data) {
        if (isReset())
            // An async query came in while the loader is stopped
            return;

        this.data = data;

        super.deliverResult(data);
    }

    @Override
    protected void onStartLoading() {
        if (data != null)
            deliverResult(data);

        if (takeContentChanged() || data == null)
            forceLoad();
    }

    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        cancelLoad();
    }

    @Override
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        data = null;
    }
}
采纳答案:

问题是你将android.support.v4.app.LoaderManagerandroid.app.LoaderManager混合。

我想你想使用支持库 ,因此你使用的是android.support.v4.app.Fragment类(缺少那些导入),因此你不应该这样做((android.app.LoaderManager.LoaderCallbacks<Object>)this)强制转换,但只需在android.support.v4.app.LoaderManager.LoaderCallbacks<List<Feed>>实现android.support.v4.app.LoaderManager.LoaderCallbacks<List<Feed>>接口,并在不进行android.support.v4.app.Fragment情况下传递它。

author: rbarriuso

参考更多解答: method clash from android LoaderManager ,转载请保留出处android LoaderManager中的方法冲突及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:classloader