Привет В моем приложении я использую навигационный ящик для перемещения из одного фрагмента в другой. В этом я добавил один календарный вид в один фрагмент. Всякий раз, когда я пытаюсь открыть этот фрагмент. приложение закрывается. Говоря, что приложение «...» закрыто. И мой fragment.xml являетсяAndroid CalendarView Начало работы
<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)
Я думаю, что это будет работать. –
@Moradiya Akash: теперь приложение не приближается, но не показывает календарь в фрагменте. – IndependentDev
Проверить отредактированный код это может работать –