There are 3 fragments, the first one (from which the application starts up is displayed normally), when I swap to go to the next fragments, the information inside them is not displayed. But I noticed that if I manage to snap to the next fragment before the first one loads infa (geolocation, data from the API, and while all the infa is loaded, then loading is spinning, so while it is spinning, I manage to snap, then everything works fine, all fragments work) on the second fragment there is RecycleVie. There are 2 errors in logcat: E / RecyclerView: No adapter attached; skipping layout and only its thread view. maybe the problem is this?

MainActivity

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; private CoordinatorLayout coordinatorLayout; private FusedLocationProviderClient fusedLocationProviderClient; private LocationCallback locationCallback; private LocationRequest locationRequest; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); coordinatorLayout = (CoordinatorLayout) findViewById(R.id.root_view); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Request permission Dexter.withActivity(this) .withPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION) .withListener(new MultiplePermissionsListener() { @Override public void onPermissionsChecked(MultiplePermissionsReport report) { if (report.areAllPermissionsGranted()) { buildLocationRequest(); buildLocationCallBack(); if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this); fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper()); } } @Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) { Snackbar.make(coordinatorLayout, "Permission Denied", Snackbar.LENGTH_LONG) .show(); } }).check(); } private void buildLocationCallBack() { locationCallback = new LocationCallback(){ @Override public void onLocationResult(LocationResult locationResult) { super.onLocationResult(locationResult); Common.current_location = locationResult.getLastLocation(); viewPager = (ViewPager)findViewById(R.id.view_pager); setupViewPager(viewPager); tabLayout = (TabLayout)findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); //Log Log.d("Location", locationResult.getLastLocation().getLatitude()+"/"+locationResult.getLastLocation().getLongitude()); } }; } private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(TodayWeatherFragment.getInstance(), "Today"); adapter.addFragment(ForecastFragment.getInstance(),"5 days"); adapter.addFragment(CityFragment.getInstance(), "Cities"); viewPager.setAdapter(adapter); } private void buildLocationRequest() { locationRequest = new LocationRequest(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(5000); locationRequest.setFastestInterval(3000); locationRequest.setSmallestDisplacement(10.0f); } 

}

 Фрагмент с RecycleView 

public class ForecastFragment extends Fragment {

 CompositeDisposable compositeDisposable; IOpenWeatherMap mService; TextView txt_city_name, txt_geo_coord; RecyclerView recycler_forecast; static ForecastFragment instance; public static ForecastFragment getInstance() { if(instance == null) instance = new ForecastFragment(); return instance; } public ForecastFragment() { compositeDisposable = new CompositeDisposable(); Retrofit retrofit = RetrofitClient.getInstance(); mService = retrofit.create(IOpenWeatherMap.class); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View itemView = inflater.inflate(R.layout.fragment_forecast, container, false); txt_city_name = (TextView)itemView.findViewById(R.id.txt_city_name); txt_geo_coord= (TextView)itemView.findViewById(R.id.txt_geo_coord); getForecastWeatherInformation(); return itemView; } private void getForecastWeatherInformation() { compositeDisposable.add(mService.getForecastWeatherByLatLng( String.valueOf(Common.current_location.getLatitude()), String.valueOf(Common.current_location.getLongitude()), Common.APP_ID, "metric") .subscribeOn(Schedulers.io()) .subscribe(new Consumer<WeatherForecastResult>() { @Override public void accept(WeatherForecastResult weatherForecastResult) throws Exception { displayForecastWeather(weatherForecastResult); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { Log.d("ERROR", ""+throwable.getMessage()); } }) ); } private void displayForecastWeather(WeatherForecastResult weatherForecastResult) { txt_city_name.setText(new StringBuilder(weatherForecastResult.city.name)); txt_geo_coord.setText(new StringBuilder(weatherForecastResult.city.coord.toString())); WeatherForecastAdapter adapter = new WeatherForecastAdapter(getContext(), weatherForecastResult); recycler_forecast.setAdapter(adapter); } 

}

    1 answer 1

    .subscribeOn (Schedulers.io ())

    then you say that you will subscribe to getForecastWeatherByLatLng in the IO pool of streams, and at the same time you don’t specify in which stream you want to get the result, as a result you receive and process in the same streams and interact with your UI only main stream. Add between the lines:

      .subscribeOn(Schedulers.io()) .subscribe(new Consumer<WeatherForecastResult>() { 

    an instruction to process the result in the main thread:

      .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<WeatherForecastResult>() { 

    And in theory, everything should be earned, I do not see any other mistakes.