0

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?

3 Answers3

0

You can create a static singleton class to hold data and retrieve that data in the fragment.

public class DataHolder {

   //create static variables here.

}

In your fragment simply retrieve the data and make it null after that.

Alternative solution

Another solution is to use Bundles. Follow the bundle solution here.

Taseer
  • 3,432
  • 3
  • 16
  • 35
0

You must cast RecyclerView.

in your fragment and onCreateView method :

recyclerView = (RecyclerView) rootView.findViewById(R.id.hour_list);
Mostafa Azadi
  • 2,893
  • 2
  • 12
  • 19
0

I know this is an old question, but contrary to the accepted answer I think the issue is that the handle to the fragment's widget such as recyclerView is not available in the OnCreateView function.

The handles (or pointers if you wish) to the widgets are only guaranteed to exist in the OnViewCreated function.

This documentation has good description of the lifecycle of fragments: https://guides.codepath.com/android/Creating-and-Using-Fragments

an extract is:

// This event is triggered soon after onCreateView().
// onViewCreated() is only called if the view returned from onCreateView() is non-null.
// Any view setup should occur here.  E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   ListView lv = (ListView) view.findViewById(R.id.lvSome);
   lv.setAdapter(adapter);
}

Hope this helps.

PhilW
  • 457
  • 6
  • 15