Linked Questions

Popular Questions

RecyclerView is a null in fragment

Asked by At

I have MainActivity,which contain viewpager,which contain 2 fragments:

MainActivity

ViewPager viewPager =  findViewById(R.id.viewPager);
    MyPagerAdapter adapter = new MyPagerAdapter(this,
            getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    TabLayout tabLayout = findViewById(R.id.TabLayout);
    tabLayout.setupWithViewPager(viewPager);
    Fragment fragmentHour=adapter.getItem(1);
    onChangeDayListener2= (OnChangeDayListener) fragmentHour;

I am also have interface for send data from main activity in fragment:

    public interface OnChangeDayListener{
    void OnChange(WeatherList list,int pos);
}

but when i try to work with this callback in interface my recycler adapter always is null:

HourWeatherFragment:

public class HourWeatherFragment extends Fragment implements MainScreen.OnChangeDayListener{
private RecyclerView recyclerView;
private AdapterHour adapter;
private ArrayList<Weather> days;
Context context;
static HttpClient httpClient;
public HourWeatherFragment() {
    // Required empty public constructor
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    days=new ArrayList<>();
    adapter = new AdapterHour(days,getActivity());
    LinearLayoutManager LayoutManager = new LinearLayoutManager(getActivity().getBaseContext(),
            LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(LayoutManager);
    recyclerView.setAdapter(adapter);
    days.add(new Weather());
    recyclerView.getAdapter().notifyDataSetChanged();
    Log.d("recycler","adapter seted!");
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_hour_weather, container, false);
    recyclerView = rootView.findViewById(R.id.hour_list);
    httpClient=new HttpClient();

    MyAsyncTask task=new MyAsyncTask();
     //   task.execute("lviv");
    return rootView;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context=context;
}

@Override
public void OnChange(WeatherList list,int pos) {
    String date=list.getAverageWeather().get(pos).getDate();
    ArrayList<Weather> weather=list.getByDate(date);
    Log.d("ldata","size of weather for adding="+weather.size());
   // days.add(new Weather());
    if(adapter!=null){
    adapter.notifyDataSetChanged();
        Log.d("tag","first null!");}
    else{
        try{
        recyclerView.getAdapter();}
        catch (NullPointerException ex){
            Log.d("tag","null!");//THERE ALWAYS NULL
        }}

So,firstly creating activity,then i start load data and when data loaded i send data from MainActivity to HourWeatherFragment throught callback, but when i receive data in fragment,my adapter is null,how i can fix it?

Related Questions