0

I have problems trying to pass a custom object from Fragment to another Fragment so I can retrieve the data and add it to my Recyclerview. Fragment A:

    DarkSkyWeather fore = DarkSkyWeather.fromJson(response, i, timestamp, i);
    Forecast_Day_Fragment fdf = new Forecast_Day_Fragment();
    fdf.addItemsRecyclerForecast(fore);

Fragment B:

public class Forecast_Day_Fragment extends Fragment{

private RecyclerView.LayoutManager mForecastLayoutManager;
private RecyclerView mForecastRecyclerView;
public ArrayList<Forecast_Daily_Item> mForecast_daily_items = new ArrayList<>();
public Adapter_Daily_Forecast mDayForecast;
private static final String TAG = "Forecast_Day_Fragment";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.forecast_layout, container, false);

    mForecastRecyclerView = view.findViewById(R.id.fore_recycle);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mForecastRecyclerView.getContext(), DividerItemDecoration.VERTICAL);
    mForecastLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    mDayForecast = new Adapter_Daily_Forecast(mForecast_daily_items);
    mForecastRecyclerView.setLayoutManager(mForecastLayoutManager);
    mForecastRecyclerView.addItemDecoration(dividerItemDecoration);
    mForecastRecyclerView.setAdapter(mDayForecast);

    return view;
}

public void addItemsRecyclerForecast(DarkSkyWeather skyWeather) {
   mForecast_daily_items.add(new Forecast_Daily_Item(R.drawable.sunny, skyWeather.getDailyTime(), skyWeather.getDailyTempLow(), skyWeather.getDailyTempHigh(), skyWeather.getDailySummary()));
    mDayForecast.notifyDataSetChanged(); //NullpointerException
}

}

Exception:

    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.aa.bb.Adapter_Daily_Forecast.notifyDataSetChanged()' on a null object reference
                                                                   at com.aa.bb.Forecast_Day_Fragment.addItemsRecyclerForecast(Forecast_Day_Fragment.java:51)
                                                                   at com.aa.bb.WeatherController$6.onSuccess(WeatherController.java:620)
                                                                   at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:152)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:168)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5845)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

I relative new to android programming and I never worked with Fragments before. Thanks for your help!

Edit:

MainActivity:

    public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d(TAG, "onCreate: Started");

    mViewPager = findViewById(R.id.viewpager_main);
    setUpViewPager(mViewPager);

}

private void setUpViewPager(ViewPager viewPager) {
    SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
    adapter.addFragment(new WeatherController());
    adapter.addFragment(new Forecast_Day_Fragment());
    adapter.addFragment(new MapsActivity());
    viewPager.setAdapter(adapter);
}

}

2 Answers2

0

The function addItemsRecyclerForecast gets called before creating the view.

So when addItemsRecyclerForecast is called, onCreateView hasn't been called yet.

Johey
  • 275
  • 1
  • 8
0

Root cause:

Forecast_Day_Fragment fdf = new Forecast_Day_Fragment();

At this time, fdf is just an object in memory. So all callback methods of fragment such as onCreate, onCreateView (in this method you init adapter), etc... are not called yet. That why the next line.

fdf.addItemsRecyclerForecast(fore);

This line try to access the adapter in Fragment B, but it not initialized yet.

Solution: Pass data (in your case is fore) from FragmentA to MainActivity, then MainActivity will forward the data to FragmentB. You should read this link to see how to pass data between fragments in viewpager.

Son Truong
  • 13,661
  • 5
  • 32
  • 58