-1

I want to use CalenderView on my activity, I also try to clean my project but this problem not solved.

This is my code:

Cal_view.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="264dp" />

</LinearLayout> 

Main.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.malik.auealert.HistoryActivity">

    <include
        android:id="@+id/calendarView"
        layout="@layout/cal_view" />

</RelativeLayout>

ActivityClass:

public CalendarView c;
// public TextView t;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    // LinearLayout parent = (LinearLayout) View v;
    /* ***ERROR Appear on here***: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.CalendarView */
    c = (CalendarView) findViewById(R.id.calendarView);
    //t = (TextView) findViewById(R.id.message);

    c.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

        }
    });

I searched Google for a solution, but couldn't find one.

Hatchet
  • 5,320
  • 1
  • 30
  • 42
Omore
  • 614
  • 6
  • 18
  • `R.id.calendarView` is not the correct id of your calendarview, in fact your calendarview does not even have an id – tyczj Mar 11 '16 at 19:19
  • 1
    If you include a `LinearLayout`, then assign to it the id `calendarView`, then look that view up and cast it to a `CalendarView`, when the error says "cannot be cast to CalendarView", what do you think is the problem? – David Medenjak Mar 11 '16 at 19:20
  • @DavidMedenjak Thanks for your reply I want to know how this problem can be solved ? – Omore Mar 11 '16 at 19:24
  • @tyczj Thanks for your reply, so how we can get CalenderView in our Activity if it has no id? – Omore Mar 11 '16 at 19:25
  • you put an id in and then use it.... – tyczj Mar 11 '16 at 19:26
  • @tyczj Thanks alot it's work now. – Omore Mar 11 '16 at 19:58

2 Answers2

1

Try with giving android:id to CalenderView and map that id with CalenderView in ActivityClass.

<CalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="264dp" />
Shashank singh
  • 641
  • 3
  • 14
0

first assign id to calendarView like below example.

<CalendarView android:id="@+id/calendarView1" android:layout_width="match_parent" android:layout_height="264dp" />

than find your included view as shown below.

View includedView = findViewById(R.id.calendarView);

CalendarView = (CalendarView) includedView.findViewById(R.id.calendarView1);

user3383787
  • 68
  • 1
  • 2
  • 6