这是一个奇怪的。昨天我在一个简单的空默认android项目中添加了以下库作为我的模块级gradle的依赖项:
https://github.com/recruit-mp/LightCalendarView
我将视图添加到活动布局文件中:
<?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>
在Nexus 5X API 25仿真器上运行该应用程序。它工作正常,月视图显示就像在GitHub上一样。
然后今天,我在家工作。我在家庭环境中做了完全相同的事情,并且当活动视图膨胀时我得到NoSuchMethodException。缺少的方法是MonthView类中的构造函数。这是完整的堆栈跟踪:
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)
当我查看库中的违规类的源代码MonthView时,我发现它确实缺少必需的构造函数:
/*
* Copyright (C) 2016 RECRUIT MARKETING PARTNERS CO., LTD.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jp.co.recruit_mp.android.lightcalendarview
import android.content.Context
import android.view.ViewGroup
import android.widget.LinearLayout
import jp.co.recruit_mp.android.lightcalendarview.accent.Accent
import java.util.*
/**
* 月カレンダーを表示する {@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})"
}
昨天怎么样? 2次之间没有对库的源代码进行任何更改。可能不同的唯一可能是我的家庭环境与我的工作环境(即我的android studio设置)。