InflateException ( error inflating class TextView) with AS H
2024-04-17 13:45

I just upgraded Android Studio to Hedgehog and get now an InflateException which did not occur before. The call stack shows that the exception occurs during the call to **setContentView **in the activity's OnCreate method. What changed with Hedgehog causing this problem?


Result:

FATAL EXCEPTION: main Process: de. myapp.myapp_1, PID: 5888 java.lang.RuntimeException: Unable to start activity ComponentInfo{de.myapp.myapp_1/de. myapp.myapp_1.act.MyAppActivity}: android.view.InflateException: Binary XML file line #111 in de. myapp.myapp_1:layout/activity_myappmain: Binary XML file line #111 in de. myapp.myapp_1:layout/activity_myappmain: Error inflating class TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: android.view.InflateException: Binary XML file line #111 in de. myapp.myapp_1:layout/activity_myappmain: Binary XML file line #111 in de. myapp.myapp_1:layout/activity_myappmain: Error inflating class TextView Caused by: android.view.InflateException: Binary XML file line #111 in de. myapp.myapp_1:layout/activity_myappmain: Error inflating class TextView Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f04010f a=-1} at android.content.res.TypedArray.getColorStateList(TypedArray.java:597) at android.widget.TextView.readTextAppearance(TextView.java:3989) at android.widget.TextView.< init>(TextView.java:1089) at android.widget.TextView.< init>(TextView.java:990) at androidx.appcompat.widget.AppCompatTextView.< init>(AppCompatTextView.java:113) at androidx.appcompat.widget.AppCompatTextView.< init>(AppCompatTextView.java:108) at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:228) at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:148) at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1678) at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1729) 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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:775) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:197) at de. myapp.myapp_1.act.MyAppActivity.onCreate(MyAppActivity.java:258) at android.app.Activity.performCreate(Activity.java:7994) at android.app.Activity.performCreate(Activity.java:7978) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I did already an "Invalidate Caches and Restart" (which was necessary to get rid of errors displayed in the Manifest file). XML Code for the TextView:

< TextView android:id="@+id/tvTestFunc" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false" android:gravity="start" android:minHeight="64sp" android:text="tvFuncTest" **android:textColor="?attr/colorOnBackground"** android:typeface="normal" />

If I remove the "android:textcolor" line from the above XML, the TextView is processes correctly (as it did with AS 2022).



Answer 1 :

There is a mistake by you that is you've typed **android:textColor="?attr/colorOnBackground"** instead of android:textColor="?attr/colorOnBackground" so just remove the ** from start and end. Here is the corrected code

< TextView android:id="@+id/tvTestFunc" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false" android:gravity="start" android:minHeight="64sp" android:text="tvFuncTest" android:textColor="?attr/colorOnBackground" android:typeface="normal" />
other answer :

The InflateException youre encountering during the inflation of your activitys layout is likely caused by a problem with a TextView element defined in your XML layout file. This could be due to various reasons, such as incorrect XML syntax, missing resources, or changes in the layout that are incompatible with the current configuration.

Since you mentioned that this issue occurred after upgrading Android Studio to version Hedgehog, its possible that the upgrade introduced changes that exposed existing issues in your layout or resource files.

To troubleshoot and resolve this issue, here are some steps you can take:

Check XML Layout Files: Go to the XML layout file referenced in the error message (activity_myappmain.xml) and locate the TextView element at line #111. Ensure that there are no syntax errors, missing attributes, or references to non-existent resources.

Review Changes: Review any recent changes you made to the layout file or related resources. Pay attention to any modifications you made after upgrading to Android Studio Hedgehog, as these changes might have introduced the issue.

Verify Resources: Make sure that all resources (e.g., strings, dimensions, drawables) referenced in the layout file exist and are correctly defined.

Inspect Stack Trace: Look for specific error messages or warnings in the stack trace that might provide more insight into the cause of the InflateException. Sometimes, additional error messages can pinpoint the exact problem.

Revert Changes: If you recently made changes to the layout file or related resources, try reverting those changes and see if the issue persists. This can help identify whether the problem is related to recent modifications.

Run Lint Checks: Use Android Studios Lint tool to perform static code analysis on your layout files. Lint can identify potential issues and provide suggestions for fixing them.

Validate XML: Use an XML validator to ensure that your layout file adheres to the XML specification and does not contain any syntax errors.

By following these steps and carefully examining your layout files, you should be able to identify and resolve the issue causing the InflateException during inflation of your activitys layout. If youre still unable to resolve the problem, consider providing the XML layout file (particularly the section around line #111) for further assistance.