我当时使用带有Retrofit和Gson的TMDB电影API实现Android分页。一个名为“最受好评的电影”的标签将显示TMDB API分页显示的最受好评的电影图像。

日志:

FATAL EXCEPTION: main
                                               Process: me.saidur.movietune, PID: 23778
                                               java.lang.ClassCastException: me.saidur.movietune.activity.MovieTuneActivity cannot be cast to me.saidur.movietune.utils.PaginationAdapterCallback
                                                   at me.saidur.movietune.fragments.PaginationAdapter.<init>(PaginationAdapter.java:53)
                                                   at me.saidur.movietune.fragments.TopRated.onCreateView(TopRated.java:117)


TopRated.java片段类文件如下:

public class TopRated extends Fragment implements PaginationAdapterCallback {

private static final String TAG = NewRelease.class.getSimpleName();
// Inset your themoviedb.org api key
private final static String API_KEY = "...";


List<Movie> results;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_top_rated, container, false);

    progressBar = (ProgressBar) v.findViewById(R.id.main_progress);
    errorLayout = (LinearLayout) v.findViewById(R.id.error_layout);
    btnRetry = (Button) v.findViewById(R.id.error_btn_retry);
    txtError = (TextView) v.findViewById(R.id.error_txt_cause);



  adapter = new PaginationAdapter(getContext()); //问题在这里提出


    recyclerView = (RecyclerView) v.findViewById(R.id.movies_recycler_view);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);

    recyclerView.setAdapter(adapter);

    recyclerView.addOnScrollListener(new PaginationScrollListener(linearLayoutManager) {
        @Override
        protected void loadMoreItems() {
            isLoading = true;
            currentPage += 1;

            loadNextPage();
        }

        @Override
        public int getTotalPageCount() {
            return TOTAL_PAGES;
        }

        @Override
        public boolean isLastPage() {
            return isLastPage;
        }

        @Override
        public boolean isLoading() {
            return isLoading;
        }
    });

    //init service and load data
    apiService = ApiClient.getClient().create(ApiInterface.class);

    loadFirstPage();

    btnRetry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loadFirstPage();
        }
    });

    return v;
}

@Override
public void retryPageLoad() {
    loadNextPage();
}


private void loadFirstPage() {
    Log.d(TAG, "loadFirstPage: ");

   .....
   ....
}

private void loadNextPage() {
    .....
    .....
}


}

PaginationAdapter.java文件如下:

公共类PaginationAdapter扩展了RecyclerView.Adapter {

// View Types
private static final int ITEM = 0;
private static final int LOADING = 1;
private static final int HERO = 2;

private static final String BASE_URL_IMG = "https://image.tmdb.org/t/p/w150";

private List<Movie> movieResults;
private Context context;

private boolean isLoadingAdded = false;
private boolean retryPageLoad = false;

private PaginationAdapterCallback mCallback;

private String errorMsg;

public PaginationAdapter(Context context) {
    this.context = context;



  this.mCallback =(PaginationAdapterCallback)context; //引发问题
  这里


    movieResults = new ArrayList<>();
}

public List<Movie> getMovies() {
    return movieResults;
}

public void setMovies(List<Movie> movieResults) {
    this.movieResults = movieResults;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());


....................
....................
...........(更多代码)

我无法弄清为什么发生崩溃的问题。如何解决该问题?

提前致谢。

最佳答案

adapter = new PaginationAdapter(getContext());


getContext()片段调用的TopRated实际上返回一个MovieTuneActivity对象,正如您所说,该对象未实现接口PaginationAdapterCallback。这就是为什么您会收到此异常。

由于TopRated Fragment实现了此接口,因此我认为您想要做的是:


在您的PaginationAdapter类中,向构造函数添加第二个参数:

public PaginationAdapter(Context context, PaginationAdapterCallback callback) {
    this.context = context;
    this.mCallback = callback;
    movieResults = new ArrayList<>();
}

在您的TopRated片段中,将其实例传递给适配器,因为片段实现了PaginationAdapterCallback接口:

adapter = new PaginationAdapter(getContext(), this);



那应该做的工作。虽然我不确定在Adapter中存储对Fragment的引用是一个好主意。

09-19 02:43