我正在尝试制作一个自定义的android日历应用程序,以添加现有应用程序的新缺失功能。我想添加一种功能,使用户可以更改年份,因为他可以更改日期和月份,但是现在出现此错误:
public CalendarCustomView(Context context) {
super(context);
}
public CalendarCustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
initializeUILayout();
setUpCalendarAdapter();
setPreviousButtonClickEvent();
setNextButtonClickEvent();
setGridCellClickEvents();
Log.d(TAG, "I need to call this method");
}
public CalendarCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initializeUILayout(){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.calendar_layout, this);
previousButton = (ImageView)view.findViewById(R.id.previous_month);
nextButton = (ImageView)view.findViewById(R.id.next_month);
currentDate = (TextView)view.findViewById(R.id.display_current_date);
addEventButton = (Button)view.findViewById(R.id.add_calendar_event);
calendarGridView = (GridView)view.findViewById(R.id.calendar_grid);
}
private void setPreviousButtonClickEvent(){
previousButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cal.add(Calendar.MONTH, -1);
setUpCalendarAdapter();
}
});
}
private void setNextButtonClickEvent(){
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cal.add(Calendar.MONTH, 1);
setUpCalendarAdapter();
}
});
}
private void setGridCellClickEvents(){
calendarGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "Clicked " + position, Toast.LENGTH_LONG).show();
}
});
}
private void setUpCalendarAdapter(){
List<Date> dayValueInCells = new ArrayList<Date>();
mQuery = new DatabaseQuery(context);
List<EventObjects> mEvents = mQuery.getAllFutureEvents();
Calendar mCal = (Calendar)cal.clone();
mCal.set(Calendar.DAY_OF_MONTH, 1);
int firstDayOfTheMonth = mCal.get(Calendar.DAY_OF_WEEK) - 1;
mCal.add(Calendar.DAY_OF_MONTH, -firstDayOfTheMonth);
while(dayValueInCells.size() < MAX_CALENDAR_COLUMN){
dayValueInCells.add(mCal.getTime());
mCal.add(Calendar.DAY_OF_MONTH, 1);
}
Log.d(TAG, "Number of date " + dayValueInCells.size());
String sDate = formatter.format(cal.getTime());
currentDate.setText(sDate);
mAdapter = new GridAdapter(context, dayValueInCells, cal, mEvents);
calendarGridView.setAdapter(mAdapter);
}
}
我的XML组件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_custom_calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@color/colorAccent"
android:orientation="horizontal">
<ImageView
android:id="@+id/previous_month"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_arrow_left" />
<TextView
android:id="@+id/display_current_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/colorPrimaryDark"
android:text="@string/Current_date"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="3"/>
<ImageView
android:id="@+id/next_month"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_arrow_right"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/add_calendar_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2018"
android:textSize="12sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/sun"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/Sun"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:id="@+id/mon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/Mon"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:id="@+id/tue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/tus"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:id="@+id/wed"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/win"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:id="@+id/thu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/ther"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:id="@+id/fri"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/FrI"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:id="@+id/sat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:text="@string/Sat"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
<GridView
android:id="@+id/calendar_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="7" />
</LinearLayout>
最后我得到的错误:
原因:android.view.InflateException:二进制XML文件第9行:二进制XML文件第9行:膨胀类com.example.meslim.datessc.Activities.CalendarCustomView时出错。 引起原因:android.view.InflateException:二进制XML文件第9行:膨胀类com.example.meslim.datessc.Activities.CalendarCustomView
时出错 原因:java.lang.ClassNotFoundException:在路径:DexPathList [[zip文件“ /data/app/com.example.meslim.materialcalendar-n8O1N1I7885zZEqtbhJnmA == / base.apk“],nativeLibraryDirectories = [/ data / app / com.example.meslim.materialcalendar-n8O1N1I7885zZEqtbhJnmA == / lib / arm64,