如何引用实现接口的活动?

时间:2017-07-18 10:17:49

标签: java android

它说错了第三个参数,我在mainActivity中实现了LoaderManager.LoaderCallbacks。

getSupportLoaderManager().initLoader(LOADER_ID,getUrlBundle(),this);

为什么会这样?

    public class MainActivity extends AppCompatActivity implements MovieAdapter.movieAdapterClickHandler,LoaderManager.LoaderCallbacks<List<Movie>> {
    ...
@Override

        protected void onCreate(Bundle savedInstanceState) {
    ...


   getSupportLoaderManager().initLoader(LOADER_ID,getUrlBundle(),this);
    }
    ...

@Override
    public Loader<List<Movie>> onCreateLoader(int i, final Bundle bundle) {
        return new AsyncTaskLoader<List<Movie>>(this) {
            @Override
            protected void onStartLoading() {
                if(bundle==null)
                    return;
                progressBar.setVisibility(View.VISIBLE);
                errorMessage.setVisibility(View.INVISIBLE);
                forceLoad();

            }

            @Override
            public List<Movie> loadInBackground() {
                /* Extract the url from the args using our constant */
                String UrlString = bundle.getString(URL_EXTRA);

                /* If the user didn't enter anything, there's nothing to search for */
                if (UrlString == null || TextUtils.isEmpty(UrlString)) {
                    return null;
                }

                return QueryUtils.fetchMovieData(UrlString);
            }

            @Override
            public void deliverResult(List<Movie> data) {
                super.deliverResult(data);
            }
        };



    }

    @Override
    public void onLoadFinished(Loader<List<Movie>> loader, List<Movie> movies) {
        if (movies != null && !movies.isEmpty()) {
            movieAdapter.MovieAdapterUpdate(movies);
            progressBar.setVisibility(View.INVISIBLE);
        } else {
            progressBar.setVisibility(View.INVISIBLE);
            errorMessage.setText("No data returned");
            errorMessage.setVisibility(View.VISIBLE);

        }
    }


    @Override
    public void onLoaderReset(Loader<List<Movie>> loader) {

    }
    }

2 个答案:

答案 0 :(得分:1)

如果您对正在使用的Loader类使用了错误的导入,则会发生这种情况。例如,如果您正在使用

import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;

一起
import android.app.LoaderManager;

然后,当您将支持依赖项与非支持依赖项混合时,您将得到错误的第三个参数警告。虽然这样可行:

import android.support.v4.app.LoaderManager;

答案 1 :(得分:0)

来自Android Document

initLoader(int id, Bundle args, LoaderCallbacks<D> callback)

您需要传递加载程序回调