gpt4 book ai didi

android - Toast.makeText 给出 "Error inflating class Textview"异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:50 26 4
gpt4 key购买 nike

我的应用程序中的每个 Toast.makeText 都会给我一个类似于此的堆栈跟踪:

/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.application, PID: 8843
android.view.InflateException: Binary XML file line #27: Binary XML file line #27: Error inflating class TextView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.widget.Toast.makeText(Toast.java:263)
at com.myapp.application.activities.ReportActivity$5.success(ReportActivity.java:240)
at com.myapp.application.activities.ReportActivity$5.success(ReportActivity.java:236)
at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.widget.Toast.makeText(Toast.java:263) 
at com.myapp.application.activities.ReportActivity$5.success(ReportActivity.java:240) 
at com.myapp.application.activities.ReportActivity$5.success(ReportActivity.java:236) 
at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=16; index=1191
at android.content.res.StringBlock.get(StringBlock.java:65)
at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1151)
at android.content.res.TypedArray.getString(TypedArray.java:195)
at android.support.v7.widget.TintTypedArray.getString(TintTypedArray.java:142)
at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:243)
at android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:165)
at android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:80)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:70)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1016)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1073)

这些是导致它的一些示例 Toast.makeText 调用

Toast.makeText(ReportActivity.this, "Reported for "+ reportType, Toast.LENGTH_SHORT).show();

Toast.makeText(this, R.string.network_unavailable, Toast.LENGTH_SHORT).show();

这一切都始于我们升级 Gradle 库并开始使用这些版本

compileSdkVersion 27
buildToolsVersion '27.0.1'

最佳答案

该错误显然是在 API 25 及更低版本上使用支持库 26+ 时出现的,据推测已在 Oreo 中修复。

参见:https://issuetracker.google.com/issues/64053344

在我的案例中,解决方法是更改​​在我的默认主题中定义的 fontFamily,方法是将 sans-serif-light 提取到一个字符串资源,这是从这里开始的:

<style name="AppCompatTheme" parent="android:...">
<item name="fontFamily">sans-serif-light</item>
</style>

对此:

   <style name="AppCompatTheme" parent="android:...">
<item name="fontFamily">@string/default_font</item>
</style>

在我的 strings.xml 中:

   <string name="default_font" translatable="false">sans-serif-light</string>

关于android - Toast.makeText 给出 "Error inflating class Textview"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48152659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com