I need to pass the string curDate between NewDateFragment and NewEventFrament, i see many peoples using Bundle, but using these i still having NullPointerException.
I transform the CalendarView to one string named curDate.
public class NewDateFragment extends Fragment {
public String curDate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newdate,
container, false);
CalendarView calendar = (CalendarView) view.findViewById(R.id.calendarView);
//sets the listener to be notified upon selected date change.
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
curDate = day + "/" + month + "/" + year;
NewDateFragment fragment = new NewDateFragment();
Bundle bundle = new Bundle();
bundle.putString("date", curDate);
fragment.setArguments(bundle);
Log.d("Current Date:", curDate);
}
});
}
public class NewEventFragment extends Fragment {
// relative code inside onCreateView
Bundle b = getActivity().getIntent().getExtras();
final String dDate = b.getString("date");
}
My Logcat:
10-08 18:34:49.036 10293-10293/com.org.feedme.cisvmeeting.activities W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41640d88) 10-08 18:34:49.056 10293-10293/com.org.feedme.cisvmeeting.activities E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.org.feedme.cisvmeeting.activities, PID: 10293 java.lang.NullPointerException at com.org.feedme.fragments.NewEventFragment.attemptCreate(NewEventFragment.java:116) at com.org.feedme.fragments.NewEventFragment$1.onClick(NewEventFragment.java:61) at android.view.View.performClick(View.java:4569) at android.view.View$PerformClick.run(View.java:18570) at android.os.Handler.handleCallback(Handler.java:743) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5212) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at dalvik.system.NativeStart.main(Native Method)
Thanks for all the help!
If they're fragments in the same activity, you could very easily access data between them using the activity.
Step 1: Declare a String curDate in your
Activity
:Step 2: In your
NewDateFragment
, inside theonSelectedDayChange()
, set the activity's curDate to the current date you just calculated:Step 3: In your
NewEventFragment
, simply get the value from the activity:An alternative to the given answer would be to use Events. If you really want to avoid coupling your code - meaning getting rid of unnecessary dependence between classes, having a variable inside your Activity is not a good idea. Here is my suggestion:
Add EventBus library to your Gradle file:
Create a simple plain old java class to represent your event:
Inside your first fragment where a date is picked, you can post an event to your second fragment as soon as the date is selected like this:
Finally, inside your second fragment, do the following:
At this point, you might be asking, why all the hussle to have all these code; but the answer is simple: the activity doesn't have to really know what is happening in either fragments. You have eliminated unnecessary coupling in your code.
If you ever change the activity to do something else, you won't have to change the fragment code.
I hope this helps you see the difference between the two approaches to communicating between fragments!
The first approach (the answer you accepted, involves 3 parties while the second approach involves only 2 parties). It is up to you to choose.
Enjoy!