PutLong 参数 Bundle 中的空 Long

PutLong on a null Long in argument Bundle

给定片段的新实例方法:

public static MyFragment newInstance(Long someLong) {
    Bundle args = new Bundle();
    MyFragment fragment = new WorkoutDetailsRoutineInfoFragment();
    args.putLong(ARGS_LONG, someLong);
    fragment.setArguments(args);
    return fragment;
}

如果 someLong 为空,则此操作无效。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference

即使它为空,我如何让它存储 Long?

检查是否为空,如果为空则添加-1,如果不为空则添加Long对象。很简单