嗨在我的应用程序中,我使用导航抽屉从一个片段移动到另一个片段。在此我在一个片段中添加了一个日历视图。每当我试图打开那个片段。该应用程序正在关闭。通过说不幸的是,应用程序“...”已关闭。 我的fragment.xml是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CalendarView
android:id="@+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="53dp" />
</RelativeLayout>
java文件是
public class AcademicCalendarFrament extends Fragment {
public AcademicCalendarFrament(){}
CalendarView calendar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
calendar=(CalendarView) getActivity().findViewById(R.id.calendarView1);
//initializes the calendarview
initializeCalendar();
}
public void initializeCalendar() {
// TODO Auto-generated method stub
calendar = (CalendarView) getActivity().findViewById(R.id.calendarView1);
// sets whether to show the week number.
calendar.setShowWeekNumber(false);
// sets the first day of week according to Calendar.
// here we set Monday as the first day of the Calendar
calendar.setFirstDayOfWeek(2);
//The background color for the selected week.
calendar.setSelectedWeekBackgroundColor(getActivity().getResources().getColor(R.color.green));
//sets the color for the dates of an unfocused month.
calendar.setUnfocusedMonthDateColor(getActivity().getResources().getColor(R.color.transparent));
//sets the color for the separator line between weeks.
calendar.setWeekSeparatorLineColor(getActivity().getResources().getColor(R.color.transparent));
//sets the color for the vertical bar shown at the beginning and at the end of the selected date.
calendar.setSelectedDateVerticalBar(R.color.darkgreen);
//sets the listener to be notified upon selected date change.
calendar.setOnDateChangeListener(new OnDateChangeListener() {
//show the selected date as a toast
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getActivity().getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
}
});
}
}
我的logcat错误是,
10-14 17:32:59.345: E/AndroidRuntime(31041): FATAL EXCEPTION: main
10-14 17:32:59.345: E/AndroidRuntime(31041): Process: com.example.everwinvidhyashram, PID: 31041
10-14 17:32:59.345: E/AndroidRuntime(31041): java.lang.NullPointerException
10-14 17:32:59.345: E/AndroidRuntime(31041): at com.example.everwinvidhyashram.AcademicCalendarFrament.onActivityCreated(AcademicCalendarFrament.java:34)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.app.Fragment.performActivityCreated(Fragment.java:1708)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.app.BackStackRecord.run(BackStackRecord.java:684)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.os.Handler.handleCallback(Handler.java:733)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.os.Handler.dispatchMessage(Handler.java:95)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.os.Looper.loop(Looper.java:136)
10-14 17:32:59.345: E/AndroidRuntime(31041): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-14 17:32:59.345: E/AndroidRuntime(31041): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 17:32:59.345: E/AndroidRuntime(31041): at java.lang.reflect.Method.invoke(Method.java:515)
10-14 17:32:59.345: E/AndroidRuntime(31041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-14 17:32:59.345: E/AndroidRuntime(31041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-14 17:32:59.345: E/AndroidRuntime(31041): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
尝试这样,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
calendar=(CalendarView) rootView.findViewById(R.id.calendarView1);
initializeCalendar();
return rootView;
}
删除onActivityCreated()方法 并删除calendar =(CalendarView)getActivity()。findViewById(R.id.calendarView1); 来自initializeCalendar()方法