recruit-mp / LightCalendarView

A lightweight monthly calendar view for Android, fully written in Kotlin. Designed to meet the minimum demands for typical calendars.
Apache License 2.0
445 stars 67 forks source link

Error inflating class jp.co.recruit_mp.android.lightcalendarview.MonthView noSuchMethodException #38

Open ThomasCookDeveloperInfo opened 6 years ago

ThomasCookDeveloperInfo commented 6 years ago

I am receiving a noSuchMethodException when trying to inflate the month view. It's easy to reproduce with a blank project and adding this view to the main activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.thomascoook.testbed.MainActivity">

    <jp.co.recruit_mp.android.lightcalendarview.MonthView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </jp.co.recruit_mp.android.lightcalendarview.MonthView>

</android.support.constraint.ConstraintLayout>

Here is the part of the stack trace that I believe is important:

Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class jp.co.recruit_mp.android.lightcalendarview.MonthView
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class jp.co.recruit_mp.android.lightcalendarview.MonthView
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

And when I look at the source code for your view I can see the problem is you are not supplying the required constructors as per this StackOverflow post and and my own experience: https://stackoverflow.com/questions/11753719/how-do-i-properly-extend-a-layout-class.

The library class in question:

/**
 * 月カレンダーを表示する {@link LinearLayout}
 * Created by masayuki-recruit on 8/19/16.
 */
class MonthView(context: Context, private val settings: CalendarSettings, var month: Date) : LinearLayout(context), DayLayout.Callback {

    internal var callback: Callback? = null

    private val weekDayLayout: WeekDayLayout
    private val dayLayout: DayLayout

    init {
        orientation = LinearLayout.VERTICAL

        weekDayLayout = WeekDayLayout(context, settings)
        addView(weekDayLayout, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))

        dayLayout = DayLayout(context, settings, month).apply { callback = this@MonthView }
        addView(dayLayout, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
    }

    override fun onDateSelected(date: Date) {
        callback?.onDateSelected(date)
    }

    fun setSelectedDate(date: Date) {
        dayLayout.setSelectedDay(date)
    }

    fun setAccents(date: Date, accents: Collection<Accent>) = dayLayout.let {
        it.getDayView(date)?.setAccents(accents)
        it.invalidateDayViews()
    }

    fun setAccents(map: Map<Date, Collection<Accent>>) {
        map.forEach { it ->
            val (date, accents) = it
            dayLayout.getDayView(date)?.setAccents(accents)
        }
        dayLayout.invalidateDayViews()
    }

    interface Callback {
        fun onDateSelected(date: Date)
    }

    override fun toString(): String = "MonthView(${month})"
}

Here is the full stack trace:

11-08 12:26:16.061 4539-4539/com.thomascoook.testbed E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thomascoook.testbed, PID: 4539
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thomascoook.testbed/com.thomascoook.testbed.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class jp.co.recruit_mp.android.lightcalendarview.MonthView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class jp.co.recruit_mp.android.lightcalendarview.MonthView
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class jp.co.recruit_mp.android.lightcalendarview.MonthView
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2204)
at java.lang.Class.getConstructor(Class.java:1683)
at android.view.LayoutInflater.createView(LayoutInflater.java:618)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.thomascoook.testbed.MainActivity.onCreate(MainActivity.kt:10)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)