This is the start page of my app and everything looks fine, but as soon as i run the app it closes.
FATAL EXCEPTION: main Process: com.bookreader.android, PID: 9970 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bookreader.android/com.bookreader.android.LauncherActivity}: android.view.InflateException: Binary XML file line #17 in com.bookreader.android:layout/activity_launcher: Binary XML file line #17 in com.bookreader.android:layout/activity_launcher: Error inflating class TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813)
Layout preview in Android Studio
is displayed correctly
My activity_launcher.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<ImageView
android:id="@ id/app_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@ id/app_logo"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/colorPrimary"
android:textSize="22dp" />
<TextView
android:id="@ id/version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:text="V1.0"
android:textColor="#cc000000" />
</RelativeLayout>
My LauncherActivity.java
public class LauncherActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
TextView version = findViewById(R.id.version);
version.setText("V" BuildConfig.VERSION_NAME);
}
}
Theme used:
<!-- Base application theme. -->
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryVariant">@color/color_primary_variant</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/color_secondary</item>
<item name="colorSecondaryVariant">@color/color_secondary_variant</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:navigationBarColor">@color/white</item>
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
</style>
- Can anyone understand why the error occurred?
- I have changed the theme but the problem is not solved, removing the TextView from the layout solves the problem! But for what reason?
- Is there a problem with the TextView class?
- What is the solution to fix it?
- Please help if you can
CodePudding user response:
@string/app_name
or @color/colorPrimary
the problem is one of these two.
This error usually occurs when one of the resources used for the attributes
is invalid.
You have used @color/color_primary
in your theme, but you have used @color/colorPrimary
in layout!, the value of @color/colorPrimary
may be defined by another library and the value for color is invalid! Or it isn't correct to use it there.
Therefore, in the event of this type of problem, be sure to go back to the used sources and check their validity, you have given an incorrect value to attributes
.
CodePudding user response:
Check whether the resource attributes that you have provided for this textview are valid or not. This issue could come if the string resource provided for the text or the color resource provided for textColor are not valid.
I tried using your code and it worked fine for me. When I intentionally replaced the textcolor attribute value with an invalid resource(drawable for example), I received the same error.