Помогите с проблемой, пжл. Пишу приложение в adroid studio. Суть следующая: не удается присвоить значение LoaderManager с помощью реализации паттерна Singletone в методе onCreate. А также возникает ошибка при возврате значения в методе onCreateLoader. Весь код MainActivity
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<JSONObject> {
private Switch switchSort;
private RecyclerView recyclerViewPosters;
private MovieAdapter movieAdapter;
private TextView textViewTopRated;
private TextView textViewPopularity;
private MainViewModel viewModel;
private static final int LOADER_ID = 133;
private LoaderManager loaderManager;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.itemMain:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
break;
case R.id.itemFavourite:
Intent intentToFavourite = new Intent(this, FavoriteActivity.class);
startActivity(intentToFavourite);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loaderManager = LoaderManager.getInstance(this);
viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
switchSort = findViewById(R.id.switchSort);
textViewTopRated = findViewById(R.id.textViewTopRated);
textViewPopularity = findViewById(R.id.textViewPopularity);
recyclerViewPosters = findViewById(R.id.recyclerviewPosters);
recyclerViewPosters.setLayoutManager(new GridLayoutManager(this, 2));
movieAdapter = new MovieAdapter();
recyclerViewPosters.setAdapter(movieAdapter);
switchSort.setChecked(true);
switchSort.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setMethodOfSort(isChecked);
}
});
switchSort.setChecked(false);
movieAdapter.setOnPosterClickListener(new MovieAdapter.OnPosterClickListener() {
@Override
public void onPosterClick(int position) {
Movie movie = movieAdapter.getMovies().get(position);
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("id", movie.getId());
startActivity(intent);
}
});
movieAdapter.setOnReachAndListener(new MovieAdapter.OnReachAndListener() {
@Override
public void onRichEnd() {
Toast.makeText(MainActivity.this, "Конец списка", Toast.LENGTH_SHORT).show();
}
});
LiveData<List<Movie>> moviesFromLiveData = viewModel.getMovies();
moviesFromLiveData.observe(this, new Observer<List<Movie>>() {
@Override
public void onChanged(List<Movie> movies) {
movieAdapter.setMovies(movies);
}
});
}
public void onClickSetPopularity(View view) {
setMethodOfSort(false);
switchSort.setChecked(false);
}
public void onClickSetTopRated(View view) {
setMethodOfSort(true);
switchSort.setChecked(true);
}
private void setMethodOfSort(boolean isTopRated) {
int methodOfSort;
if (isTopRated) {
textViewTopRated.setTextColor(getResources().getColor(R.color.teal_200));
textViewPopularity.setTextColor(getResources().getColor(R.color.white_color));
methodOfSort = NetworkUtils.TOP_RATED;
} else {
methodOfSort = NetworkUtils.POPULARITY;
textViewPopularity.setTextColor(getResources().getColor(R.color.teal_200));
textViewTopRated.setTextColor(getResources().getColor(R.color.white_color));
}
downloadData(methodOfSort, 1);
}
private void downloadData(int methodOfSort, int page) {
URL url = NetworkUtils.buildURL(methodOfSort, page);
Bundle bundle = new Bundle();
bundle.putString("url", url.toString());
loaderManager.restartLoader(LOADER_ID, bundle, this);
}
@Override
public Loader<JSONObject> onCreateLoader(int id, Bundle args) {
NetworkUtils.JSONLoader jsonLoader = new NetworkUtils.JSONLoader(this, args);
return jsonLoader;
}
@Override
public void onLoadFinished(Loader<JSONObject> loader, JSONObject data) {
ArrayList<Movie> movies = JSONUtils.getMoviesFromJSON(data);
if (movies != null && !movies.isEmpty()) {
viewModel.deleteAllMovies();
for (Movie movie : movies) {
viewModel.insertMovie(movie);
}
}
loaderManager.destroyLoader(LOADER_ID);
}
@Override
public void onLoaderReset(Loader<JSONObject> loader) {
}
}
Ошибка возникает (выделю места ошибок в строках кода жирным) в loaderManager = LoaderManager.
getInstance(this); А также при возврате значения return
jsonLoader; в методе onCreateLoader.
Помогите разобраться с этой проблемой, пжл. Никак не могу понять, что я делаю не так?