简体   繁体   中英

Android CalendarView Getting close

Hi In my application I am using navigation drawer to move from one fragment to another. In this I added one calendar view in one fragment. Whenever I try to open that fragment. the application is getting closed. By saying Unfortunately the applicatio "..." is closed. And my fragment.xml is

<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>

And the java file is

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();

        }

    });

}
} 

And my logcat errors are,

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)

try like this,

@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;
}

remove onActivityCreated() method and also remove calendar = (CalendarView) getActivity().findViewById(R.id.calendarView1); from initializeCalendar() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM