Я знаю, ребята. Нулевого указателя было уже много. Честно говоря, я прочитал все эти темы, но не могу найти ошибку в коде. Пожалуйста, помогите.
Я использую обычную схему RxJava + дооснащение, но полученные журналы нулевого указателя говорят об этой ошибке в ответ. Может быть проблема в модели, но я проверил это. Я знаю, что что-то потерял, но не могу узнать, что именно.
Фрагмент
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* Main Initialization
*/
resultsList = new ArrayList<>();
//adapter = new ListAdapter(resultsList);
view = inflater.inflate(R.layout.fragment_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_list_detailed);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//adapter.setClickListener(this);
/**
* Checking for internet connection
*/
if (InternetConnection.checkConnection(getContext())) {
final ProgressDialog dialog;
dialog = new ProgressDialog(getContext());
dialog.setTitle(getString(R.string.string_getting_json_title));
dialog.setMessage(getString(R.string.string_getting_json_message));
dialog.show();
/**
* Making HTTP request
*/
ApiService api = RetrofitClient.getApiService();
Observable.just(api.getPhotosList(getString(R.string.api_key)))
.map(Call::execute)
.filter(Response::isSuccessful)
.map(Response::body)
.onErrorReturn(throwable -> new ResultsPojo(new ArrayList<>(), 1))
.doOnError(throwable -> Log.e("ERROR", throwable.getMessage()))
.map(ResultsPojo::getMovies)
.doOnNext(list -> {
if (!list.isEmpty()) {
adapter = new ListAdapter(list);
recyclerView.setAdapter(adapter);
} else Log.d("LISTMOVIES", "Something went wrong!!!");
}).subscribe();
ApiService
public interface ApiService {
@GET("3/movie/popular")
Call<ResultsPojo> getPhotosList(@Query("api_key") String key);
}
MoviesPojo
public class MoviesPojo {
@SerializedName("vote_count")
@Expose
private int voteCount;
@SerializedName("id")
@Expose
private int id;
@SerializedName("video")
@Expose
private boolean video;
@SerializedName("vote_average")
@Expose
private double voteAverage;
@SerializedName("title")
@Expose
private String title;
@SerializedName("popularity")
@Expose
private double popularity;
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds = null;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("adult")
@Expose
private boolean adult;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;
public int getVoteCount() {
return voteCount;
}
public void setVoteCount(int voteCount) {
this.voteCount = voteCount;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isVideo() {
return video;
}
public void setVideo(boolean video) {
this.video = video;
}
public double getVoteAverage() {
return voteAverage;
}
public void setVoteAverage(double voteAverage) {
this.voteAverage = voteAverage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPopularity() {
return popularity;
}
public void setPopularity(double popularity) {
this.popularity = popularity;
}
public String getPosterPath() {
return posterPath;
}
public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}
public String getOriginalLanguage() {
return originalLanguage;
}
public void setOriginalLanguage(String originalLanguage) {
this.originalLanguage = originalLanguage;
}
public String getOriginalTitle() {
return originalTitle;
}
public void setOriginalTitle(String originalTitle) {
this.originalTitle = originalTitle;
}
public List<Integer> getGenreIds() {
return genreIds;
}
public void setGenreIds(List<Integer> genreIds) {
this.genreIds = genreIds;
}
public String getBackdropPath() {
return backdropPath;
}
public void setBackdropPath(String backdropPath) {
this.backdropPath = backdropPath;
}
public boolean isAdult() {
return adult;
}
public void setAdult(boolean adult) {
this.adult = adult;
}
public String getOverview() {
return overview;
}
public void setOverview(String overview) {
this.overview = overview;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
}
ResultsPojo
public class ResultsPojo {
@SerializedName("page")
@Expose
private int page;
@SerializedName("total_results")
@Expose
private int totalResults;
@SerializedName("total_pages")
@Expose
private int totalPages;
@SerializedName("moviesPojos")
@Expose
private List<MoviesPojo> moviesPojos;
public ResultsPojo(List<MoviesPojo> results, int page){
this.moviesPojos = results;
this.page = page;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotalResults() {
return totalResults;
}
public void setTotalResults(int totalResults) {
this.totalResults = totalResults;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public List<MoviesPojo> getMovies() {
return moviesPojos;
}
public void setMoviesPojos(List<MoviesPojo> moviesPojos) {
this.moviesPojos = moviesPojos;
}
}
Адаптер
private List<MoviesPojo> resultList;
private ItemClickListener mClickListener;
public ListAdapter(List<MoviesPojo> data) {
this.resultList = data;
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner, parent, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListViewHolder holder, int position) {
}
@Override
public int getItemCount() {
//TODO Здесь мы должны получить список ссылок для изображений
return resultList.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView mImageBanner;
public ListViewHolder(View itemView) {
super(itemView);
mImageBanner = (ImageView) itemView.findViewById(R.id.item_banner_image);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
// public String getItem(int id) {
// return resultList.get(id);
// }
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.borisruzanov.popularmovies, PID: 10947
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.borisruzanov.popularmovies/com.borisruzanov.popularmovies.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.borisruzanov.popularmovies.adapters.ListAdapter.setClickListener(com.borisruzanov.popularmovies.adapters.ListAdapter$ItemClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.borisruzanov.popularmovies.adapters.ListAdapter.setClickListener(com.borisruzanov.popularmovies.adapters.ListAdapter$ItemClickListener)' on a null object reference
at com.borisruzanov.popularmovies.ListFragment.onCreateView(ListFragment.java:51)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6679)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609
Большое спасибо за потраченное время !!! +1 к карме