Есть 3 фрагмента, первый (с которого запускается приложение отображается нормально), когда свайпаю, чтобы перейти на следующие фрагменты, то информация внутри них не отображается.Но вот что заметил, если я успеваю свайпнуть на следующий фрагмент до того, как на первом загрузится вся инфа(геолокация, данные с API, причем пока вся инфа грузится, то крутится loading, так вот пока он круится я успеваю свайпнуть, то все работает отлично, все фрагменты работают) на втором фрагменте есть RecycleVie. В logcat есть 2 ошибки :E/RecyclerView: No adapter attached; skipping layout и Only the original thread that created a view hierarchy can touch its views. возможно проблема в этом ?
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);
}
}