Android Fragments 在父活动中更改日期时将TabLayout片段视图示例设置为null

mlmc2os5  于 2023-01-31  发布在  Android
关注(0)|答案(1)|浏览(164)

从TabLayout的父Activity中的日期选取器选择日期时,使片段视图示例为空。
下面是日期选择器方法,在选择日期后,我调用Fragment接口更新片段值getCollectionListData()。其中回收器视图示例为空。

private void showDatePicker() {
    Calendar currDate = Calendar.getInstance();
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date date = null;
    try {
        date = df.parse(mDateTV.getText().toString());
        currDate.setTime(date);
        selectedYear = currDate.get(Calendar.YEAR);
        selectedmonth = currDate.get(Calendar.MONTH);
        selectedDate = currDate.get(Calendar.DATE);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    DatePickerDialog datePickerDialog = new DatePickerDialog(this, R.style.DatePickerTheme, (DatePicker datePicker, int ofYear, int monthOfYear, int dayOfMonth) -> {
        selectedYear = ofYear;
        selectedmonth = monthOfYear;
        selectedDate = dayOfMonth;
        mDateTV.setText(selectedDate + "/" + (selectedmonth + 1) + "/" + selectedYear);
      
        mCollectionListFragment.getCollectionListData();

    }, selectedYear, selectedmonth, selectedDate);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_MONTH, -7);
    Date result = cal.getTime();
    datePickerDialog.getDatePicker().setMinDate(result.getTime());
    datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
    datePickerDialog.show();
}

第一个片段中的接口方法。

@Override
    public void getCollectionListData() {
        //listUpdated();
        mCollectionListRecyclerView.setVisibility(View.VISIBLE);
    }

正在将mCollectionListRecyclerView设置为空。

错误:mCollectionListRecyclerView上出现空指针异常。

9lowa7mx

9lowa7mx1#

获取当前片段示例并调用片段方法。

Fragment childFragment = (Fragment) mViewPagerDetailsAdapter.instantiateItem(mViewPager,mViewPager.getCurrentItem());

if (childFragment != null) {
   ((MyChildFragment) childFragment).updateView();
}

相关问题