我需要在 NewDateFragment 和 NewEventFrament 之间传递字符串 curDate,我看到很多人使用 Bundle,但使用这些我仍然有 NullPointerException。
我将 CalendarView 转换为一个名为 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");
}
我的日志猫:
10-08 18:34:49.036 10293-10293/com.org.feedme.cisvmeeting.activities W/dalvikvm: threadid=1: 线程退出未捕获异常 (group=0x41640d88) 10-08 18:34:49.056 10293-10293 /com.org.feedme.cisvmeeting.activities E/AndroidRuntime:致命异常:主进程:com.org.feedme.cisvmeeting.activities,PID:10293 java.lang.NullPointerException at com.org.feedme.fragments.NewEventFragment.attemptCreate (NewEventFragment.java:116) 在 com.org.feedme.fragments.NewEventFragment$1.onClick(NewEventFragment.java:61) 在 android.view.View.performClick(View.java:4569) 在 android.view.View$PerformClick .run(View.java:18570) 在 android.os.Handler.handleCallback(Handler.java:743) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os。Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5212) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke( Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 在 dalvik.system .NativeStart.main(本机方法)ZygoteInit.main(ZygoteInit.java:602) 在 dalvik.system.NativeStart.main(Native Method)ZygoteInit.main(ZygoteInit.java:602) 在 dalvik.system.NativeStart.main(Native Method)
感谢所有的帮助!