将Object从另一个Fragment传递给Adapter时出现NullPointerException

时间:2018-04-22 08:43:44

标签: android android-fragments android-recyclerview

我在尝试将自定义对象从Fragment传递到另一个Fragment时遇到问题,因此我可以检索数据并将其添加到Recyclerview中。 片段A:

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
}

片段B:

    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)

}

例外:

    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);
}

我相对较新的Android编程,我之前从未使用过Fragments。 谢谢你的帮助!

编辑:

MainActivity:

    <View style={styles.top}


    />
    <MapView style={styles.map}
    region ={{
    latitude:lat,
    longitude:long,
    latitudeDelta: 0.1,
    longitudeDelta: 0.1,

}

2 个答案:

答案 0 :(得分:0)

在创建视图之前调用addItemsRecyclerForecast函数。

因此,当调用addItemsRecyclerForecast时,尚未调用onCreateView。

答案 1 :(得分:0)

根本原因:

Forecast_Day_Fragment fdf = new Forecast_Day_Fragment();

此时,fdf只是内存中的一个对象。所以片段的所有回调方法,例如 onCreate onCreateView (在此方法中为init适配器)等。 ..尚未被召唤。这就是下一行的原因。

fdf.addItemsRecyclerForecast(fore);

此行尝试访问Fragment B中的适配器,但尚未初始化。

解决方案:将数据(在您的情况下是前导)从FragmentA传递到MainActivity,然后MainActivity将数据转发到FragmentB。 您应该阅读此link以了解如何在viewpager中的片段之间传递数据。