I searched and found nothing to explain why null is the result of Intent, such as:
How to get Date extra from an intent?
Android: Passing Date in putExra
It appears that Intent does not pass anything in my code as my code results in a NullPointerException (I am working through "Android Programming" by Big Nerd Ranch):
First, I declare and define the key EXTRA_DATE and create the Intent in CrimeFragment's code (NOTE: this is the TAG used in the LogCat output shown below the code), in the else statement:
public static final String EXTRA_DATE = "date";
private static Date date = null;
...
if (small screen) {
//small screen code
} else {
Log.d(TAG, "Large screen: activity");
date = mCrime.getDate();
Log.d(TAG, "date is " + date);
Intent intent = new Intent(getContext(), DatePickerActivity.class);
intent.putExtra(EXTRA_DATE, date);
startActivityForResult(intent, REQUEST_DATE);
}
As can be seen in the LogCat output for this code section (below), the 'date' variable has a value at this point, just before sending it out with Intant:
CrimeFragment: Large screen: activity
CrimeFragment: date is Mon Oct 16 18:19:24 CDT 2017
startActivityForResult then launches DatePickerActivity, and enters createFragment (Android Studio suggested that I add the import, which I did):
import static com.bignerdranch.android.criminalintent.DatePickerFragment.EXTRA_DATE;
public class DatePickerActivity extends SingleFragmentActivity {
private final String TAG = "DatePickerActivity";
private static final int REQUEST_DATE = 0;
@Override
protected Fragment createFragment() {
Log.d(TAG, "Entered createFragment");
Date date = (Date) getIntent().getSerializableExtra(EXTRA_DATE);
Log.d(TAG,"date = " + date);
return DatePickerFragment.newInstance(date);
}
As noted in the import statement, DatePickerFragment is referenced in the import statement:
public static DatePickerFragment newInstance(Date date){
Log.d(TAG, "Entered newInstance");
Log.d(TAG, "Date is " + date);
Bundle args = new Bundle();
args.putSerializable(EXTRA_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}
SingleFragmentActivity is extended:
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
private static final String TAG= "SingleFragmentActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Entered onCreate");
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
//fragment_container is the top level FrameLayout in activity_fragment
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null){
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
}
But the Intent fails immediately as shown by the LogCat line in which 'date = null':
CrimeFragment: Entered onPause
CrimeFragment: Entered onPause
SingleFragmentActivity: Entered onCreate
DatePickerActivity: Entered createFragment
DatePickerActivity: date = null
This null value is now passed to DatePickerFragment:
public class DatePickerFragment extends DialogFragment {
public static final String EXTRA_DATE = "com.bignerdranch.android.criminalintent.date";
private static final String ARG_DATE = "Date";
private static final String TAG = "DatePickerFragment";
private DatePicker mDatePicker;
public static DatePickerFragment newInstance(Date date){
Log.d(TAG, "Entered newInstance");
Log.d(TAG, "Date is " + date);
Bundle args = new Bundle();
args.putSerializable(ARG_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState){
Log.d(TAG, "Entered onCreateView");
Date date = (Date) getArguments().getSerializable(EXTRA_DATE);
Log.d(TAG, "Date equals: " + date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Until the NullPointerException happens:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bignerdranch.android.criminalintent/com.bignerdranch.
android.criminalintent.DatePickerActivity}: java.lang.NullPointerException:
Attempt to invoke virtual method ‘long java.util.Date.getTime()’ on a null
object reference
As seen above in the LogCat output for DatePickerActivity, the date variable is null and so null is passed from that point on as seen in the following LogCat snippet from the onCreateView code above.
DatePickerFragment: Entered newInstance
DatePickerFragment: Date is null
DatePickerFragment: Entered onCreateView
DatePickerFragment: Date equals: null
AndroidRuntime: Shutting down VM
Through all my reading, including all posts in stackoverflow.com, it seems that this should just work, that Intent should simply pass the Date value from CrimeFragment to DatePickerFragment, but it doesn't as the value of 'Date equals null' shows.
It seems that there's nothing more for me to do, that Intent should just pass 'date', but it doesn't.