这个问题困扰了我许久,是在使用RecyclerView的适配器时抛出的异常,不知问题出自何处的我更惊叹的是网上竟找不到任何类似问题的解决办法,最终也只是在stackoverflow找到一个基本相同的问题,但是也没有人回答,不过最后还是解决了,在这里分享一下解决的办法
报错信息如下
E/TypefaceCompatApi26Impl: Unable to collect necessary methods for class java.lang.NoSuchMethodException
java.lang.NoSuchMethodException: android.graphics.FontFamily.<init> []
at java.lang.class.getConstructor0(Class.java:2332)
at java.lang.class.getConstructor(Class.java:1728)
at androidx.core.graphics.TypefaceCompatApi26Impl.obtainFontFamilyCtor(TypefaceCompatApi26Impl.java:321)
at androidx.core.graphics.TypefaceCompatApi26Impl.<init>(TypefaceCompatApi26Impl.java:84)
at androidx.core.graphics.TypefaceCompatApi28Impl.<init>(TypefaceCompatApi28Impl.java:36)
at androidx.core.graphics.TypefaceCompat.<clinit>(TypefaceCompat.java:47)
at androidx.core.graphics.TypefaceCompat.create(TypefaceCompat.java:190)
at androidx.appcompat.widget.AppCompatTextView.setTypeface(AppCompatTextView.java:705)
at android.widget.TextView.resolveStyleAndSetTypeface(TextView.java:2183)
at android.widget.TextView.setTypefaceFromAttrs(TextView.java:2154)
at android.widget.TextView.applyTextAppearance(TextView.java:4105)
at android.widget.TextView.<init>(TextView.java:1630)
at android.widget.TextView.<init>(TextView.java:990)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:99)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:95)
at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:182)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1059)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at android.view.View.inflate(View.java:26707)
at com.example.appdemo.adapters.MessageAdapter.onCreateViewHolder(MessageAdapter.java:29)
at com.example.appdemo.adapters.MessageAdapter.onCreateViewHolder(MessageAdapter.java:15)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.linearlayoutmanager$LayoutState.next(linearlayoutmanager.java:2303)
at androidx.recyclerview.widget.linearlayoutmanager.layoutChunk(linearlayoutmanager.java:1627)
at androidx.recyclerview.widget.linearlayoutmanager.fill(linearlayoutmanager.java:1587)
at androidx.recyclerview.widget.linearlayoutmanager.onLayoutChildren(linearlayoutmanager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
指向onCreateViewHolder中的这一行
View itemView = View.inflate(mContext,R.layout.message_view, null);
程序会报异常,但却可以运行
解决方案一
修改build.gradle中的targetSdkVersion为28
targetSdkVersion 28
解决方案二
添加依赖
def core_version = "1.3.2"
用Java的话还需要
//Java
implementation "androidx.core:core:$core_version"
用Kotlin加这个
// Kotlin
implementation "androidx.core:core-ktx:$core_version"
最后同步依赖重新启动项目就可以了