Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toast.makeText Giving "Error inflating class Textview" Exception

Every single Toast.makeText in my app is giving me a stack trace similar to this:

/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)

These are some sample Toast.makeText calls that are causing it

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

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

It all started when we upgraded our Gradle libraries and started using these versions

compileSdkVersion 27
buildToolsVersion '27.0.1'
like image 724
Walker Avatar asked Jan 08 '18 14:01

Walker


3 Answers

The bug apparently occurs when using support library 26+, on API 25 and lower, and is supposedly fixed in Oreo.

See: https://issuetracker.google.com/issues/64053344

The fix in my case was to change the fontFamily defined in my default theme, by extracting sans-serif-light to a string resource, that is going from this:

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

To this:

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

with in my strings.xml:

   <string name="default_font" translatable="false">sans-serif-light</string>
like image 178
Findns Avatar answered Nov 17 '22 13:11

Findns


My Toast callings was with getBaseContext(). I just change it to getApplicationContext() to solve the problem.

like image 22
Pedro Henrique Avatar answered Nov 17 '22 12:11

Pedro Henrique


I solved the crashing from the Toast.makeText entries all throughout the app by doing a global search for fontFamily and deleting the entries.

It seems like the font family was being defined and the new build tools and SDK didn't like that.

like image 2
Walker Avatar answered Nov 17 '22 14:11

Walker