I have rewritten the code and am still getting a null pointer exception error message and can’t figure out why:
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 can be seen in the LogCat output date and id are fine:
CrimeFragment: Entered onClick
CrimeFragment: Large screen: activity
CrimeFragment: date is Mon Oct 16 18:19:24 CDT 2017
CrimeFragment: id is 5ebc3a88-caa1-46c2-a0b4-d02dc9fd6e81
That output is generated here:
{
//small screen code;ialog
} else {
Log.d(TAG, "Large screen: activity");
date = mCrime.getDate();
Log.d(TAG, "date is " + date);
id = mCrime.getId();
Log.d(TAG, "id is " + id);
Intent intent = new Intent(getContext(), DatePickerActivity.class);
intent.putExtra(EXTRA_DATE, date);
startActivityForResult(intent, REQUEST_DATE);
}
However, thought that I added ‘date’ above but it shows ‘null’:
CrimeFragment: Entered onPause
CrimeFragment: Entered onPause
SingleFragmentActivity: Entered onCreate
DatePickerActivity: Entered createFragment
DatePickerActivity: date = null
From the DatePickerActivity:
protected Fragment createFragment() {
Log.d(TAG, "Entered createFragment");
Date date = (Date) getIntent().getSerializableExtra(EXTRA_DATE);
Log.d(TAG,"date = " + date);
return DatePickerFragment.newInstance(date);
}
And, it shows ‘null’:
DatePickerFragment: Entered newInstance
DatePickerFragment: Date is null
DatePickerFragment: Entered onCreateView
DatePickerFragment: Date equals: null
AndroidRuntime: Shutting down VM
From the DatePickerFragment:
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);
What am I missing about Intents? I can’t see what I’m doing wrong with regards to sending data between activities and fragments.