从DatePickerFragment onDateSet重新加载片段

时间:2019-07-03 17:46:02

标签: android android-fragments android-dialogfragment

设置其他日期后,我试图刷新/重新加载片段。 因此,我创建了一个DatePickerFragment,它扩展了DialogFragment并实现了DatePickerDialog.OnDateSetListener

在我的onDataSet函数中,我有以下代码:

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    Fragment reload = fragmentManager.findFragmentByTag("mealsworkoutsfrag");
    Bundle bundle = new Bundle();
    bundle.putString("newdate",date);
    ft.detach(reload);
    reload.setArguments(bundle);
    ft.attach(reload);
    ft.commit();

现在,如果我运行此代码,则会在reload.setArgs行上显示错误

  

“试图在空对象引用上调用虚拟方法'void android.support.v4.app.Fragment.setArguments(android.os.Bundle)'”

在主要活动中,我像这样创建片段:

FragmentTransaction mealsworkoutsFrag = getSupportFragmentManager().beginTransaction();
        mealsworkoutsFrag.replace(R.id.flMain,new MealsWorkoutsFragment(),"mealsworkoutsfrag");
        mealsworkoutsFrag.commit();

我想念什么?

编辑:请求额外的代码

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.mealworkoutTab) {
        iconShow = true;
        invalidateOptionsMenu();
        FragmentTransaction mealsworkoutsFrag = getSupportFragmentManager().beginTransaction();
        mealsworkoutsFrag.replace(R.id.flMain,new MealsWorkoutsFragment(),"mealsworkoutsfrag");
        mealsworkoutsFrag.commit();
    } else if (id == R.id.userinfoTab) {
        iconShow = false;
        invalidateOptionsMenu();
        FragmentTransaction userInfoFrag = getSupportFragmentManager().beginTransaction();
        userInfoFrag.replace(R.id.flMain,new UserInfoFragment(),"userinfofrag");
        userInfoFrag.commit();

    } else if (id == R.id.logoutTab) {
        auth.signOut();
    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

这是日期选择器:

public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();

    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    final String d,m,y,date;

    month = month +1;

    if (String.valueOf(day).length() == 1) {
        d = "0"+day;
    }
    else {
        d = String.valueOf(day);
    }

    if (String.valueOf(month).length() == 1) {
        m = "0"+month;
    }
    else {
        m = String.valueOf(month);
    }

    y = String.valueOf(year);

    date = d+m+y;
    //EventBus.getDefault().post(new DateEvent(date));
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    Fragment reload = fragmentManager.findFragmentByTag("mealsworkoutsfrag");
    Bundle bundle = new Bundle();
    bundle.putString("newdate",date);
    ft.detach(reload);
    reload.setArguments(bundle);
    ft.attach(reload);
    ft.commit();

}

}

这就是我所说的日期选择器:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.date_picker) {

        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");

        return true;
    }

    return super.onOptionsItemSelected(item);
}

1 个答案:

答案 0 :(得分:0)

您可以共享更多代码吗?

在使用的所有逻辑中您可能会犯很多错误。例如,您的活动和片段所属的类是扩展,导入等。

我想也许使用getActivity()。getFragmentManager();在onDateSet中起作用。

另一种解决方案是在Fragment中添加侦听器实现,然后从onDateSet调用它。 (片段之间的通信)。