Toast.makeText给出“错误膨胀类Textview”异常

时间:2018-01-08 14:48:50

标签: android exception toast

我的应用中的每个 public static int readTestCaseCode(MultipartFile multipartFile) { File file = new File(//Do I need to specify a location?) multipartFile.transferTo(); Scanner scanner = new Scanner(file) } 给我一个类似于此的堆栈跟踪:

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();

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

时开始的
Toast.makeText(this, R.string.network_unavailable, Toast.LENGTH_SHORT).show();

3 个答案:

答案 0 :(得分:24)

在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>

答案 1 :(得分:2)

通过对Toast.makeText进行全局搜索并删除条目,我解决了整个应用中fontFamily个条目的崩溃问题。

似乎正在定义字体系列,新的构建工具和SDK不喜欢这样。

答案 2 :(得分:2)

我的Toast呼叫是与getBaseContext()进行的。我只是将其更改为getApplicationContext()即可解决问题。