Android 如何使用CalendarView强制转换LinearLayout?
我想在我的活动中使用Android 如何使用CalendarView强制转换LinearLayout?,android,android-activity,casting,android-linearlayout,calendarview,Android,Android Activity,Casting,Android Linearlayout,Calendarview,我想在我的活动中使用日历视图,我也尝试清理我的项目,但这个问题没有解决 这是我的代码: calu 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_hei
日历视图
,我也尝试清理我的项目,但这个问题没有解决
这是我的代码:
calu 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>
我在谷歌上搜索了一个解决方案,但没有找到。尝试将安卓:id提供给CalenderView,并在活动类中将该id映射到CalenderView
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="264dp" />
首先将id分配给calendarView,如下例所示
然后查找包含的视图,如下所示
视图includedView=findViewById(R.id.calendarView)
CalendarView=(CalendarView)includedView.findViewById(R.id.calendarView1)
R.id.calendarView
不是您的calendarView的正确id,事实上您的calendarView甚至没有IDI如果您包含一个线性布局
,然后为其分配idcalendarView
,然后向上查看该视图并将其转换为calendarView
,当错误显示“无法转换为calendarView”,您认为问题出在哪里?@DavidMedenjak谢谢您的回复我想知道如何解决这个问题?@tyczj谢谢您的回复,那么如果CalendarView没有id,我们如何在我们的活动中获取它?您将id放入并使用它。。。。
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) {
}
});
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="264dp" />