我使用以下類來顯示自定義日曆,但使用此後我得到的錯誤在膨脹類異常。使用以顯示的日曆視圖Android InflateException二進制XML文件行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_custom_calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
//getting mainactivity fetch tools:context="androidcustomcalendar.inducesmile.com.mycutomcalendar.MainActivity">
//gettting error on this Line :
//Inflating Calendar class
<androidcustomcalendar.inducesmile.com.mycutomcalendar.CalendarCustomView
android:id="@+id/custom_calendar"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidcustomcalendar.inducesmile.com.mycutomcalendar.CalendarCustomView>
CalendarCustomView類用於在活動設置日曆和表示事件表示日曆
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Getting error here for inflating
setContentView(R.layout.activity_main);
//Inflating this class from another
CalendarCustomView mView = (CalendarCustomView)findViewById(R.id.custom_calendar);
}
主活動佈局
MainActivity類別:
//Class for showing calendar on the layout using linear layout
public class CalendarCustomView extends LinearLayout{
private Button addEventButton;
//Showing Max column
private static final int MAX_CALENDAR_COLUMN = 42;
private SimpleDateFormat formatter = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
private Calendar cal = Calendar.getInstance(Locale.ENGLISH);
private Context context;
private GridAdapter mAdapter;
public CalendarCustomView(Context context) {
super(context);
}//context using CalendarCustom View
public CalendarCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
//Initalizing Ui layout
initializeUILayout();
//Set up calendar adapter
setUpCalendarAdapter();
//setting previous button clicking event for going to back
setPreviousButtonClickEvent();
//next button to move forward
setNextButtonClickEvent();
//click on cell of calendar
setGridCellClickEvents();
//Log.d(TAG, "I need to call this method");
}
public CalendarCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initializeUILayout(){
//Inflating layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//using Calendar_layout here
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);
}
//Setting previous button click event for moving back
private void setPreviousButtonClickEvent(){
previousButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cal.add(Calendar.MONTH, -1);
//Setting Calendar Adapter for events and other stuff
setUpCalendarAdapter();
}
});
}
//Showing next button to move forward
private void setNextButtonClickEvent(){
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//adding month in when click on next button
cal.add(Calendar.MONTH, 1);
//Setting up adapter to showing colors and events on calander
setUpCalendarAdapter();
}
});
}
//setting click events for grid cell
private void setGridCellClickEvents(){
calendarGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Getting onItemClick events
// Toast.makeText(context, "Clicked " + position, Toast.LENGTH_LONG).show();
}
});
}
//setting calendar adapter for showing events and other stuff of calendar
private void setUpCalendarAdapter(){
List<Date> dayValueInCells = new ArrayList<Date>();
//mQuery = new DatabaseQuery(context);
// List<EventObjects> mEvents = mQuery.getAllFutureEvents();
//Using list for adding events and showing on calendar
List<EventObjects> mEvents = new ArrayList<>();
//adding first object in list
EventObjects eventObjects = new EventObjects(1, "message", mQuery.convertStringToDate("1-02-20017"));
//adding second object in list
EventObjects eventObjects1 = new EventObjects(1, "message", mQuery.convertStringToDate("5-02-20017"));
//adding third object in list
EventObjects eventObjects2 = new EventObjects(1, "message", mQuery.convertStringToDate("6-02-20017"));
//adding fourth object in list
EventObjects eventObjects3 = new EventObjects(1, "message", mQuery.convertStringToDate("15-02-20017"));
//Adding first object in the list
mEvents.add(eventObjects);
//Adding second object in the list
mEvents.add(eventObjects1);
//adding third object in the list
mEvents.add(eventObjects3);
// adding second object in the list
mEvents.add(eventObjects2);
//the above all objects are using to show some events on the calendar
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(dayValueInCel ls.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);
//setting gridadapter class for making calender view in grid
calendarGridView.setAdapter(mAdapter);
}
}
請分享完全崩潰的logcat –
檢查,這是在正確的包像這裏'androidcustomcalendar.inducesmile.com.mycutomcalendar.CalendarCustomView' – Piyush
java.lang.RuntimeException:無法啓動活動Com ponentInfo {androidcustomcalendar.inducesmile.com.mycutomcalendar/androidcustomcalendar.inducesmile.com.mycutomcalendar.MainActivity}:android.view.InflateException:二進制XML文件行#9:錯誤充氣類 –