I'm new to Android development, so I just got the hang of Gradle and just ran my app for the first time. However, I have no idea why it keeps crashing because I don't really know how to read the error messages. I'll leave the output from logcat and my code in this post. Please help!
ERROR:
FATAL EXCEPTION: main
Process: com.example.xml_test, PID: 9417
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xml_test/com.example.xml_test.main_activity}: android.view.InflateException: Binary XML file line #108 in com.example.xml_test:layout/main: Can't convert value at index 7 to dimension: type=0x3, theme={InheritanceMap=[id=0x7f100262com.example.xml_test:style/Theme.Xml_test, id=0x7f10023acom.example.xml_test:style/Theme.MaterialComponents.DayNight.DarkActionBar, id=0x7f100253com.example.xml_test:style/Theme.MaterialComponents.Light.DarkActionBar, id=0x7f10006acom.example.xml_test:style/Base.Theme.MaterialComponents.Light.DarkActionBar, id=0x7f100068com.example.xml_test:style/Base.Theme.MaterialComponents.Light, id=0x7f1000a1com.example.xml_test:style/Base.V21.Theme.MaterialComponents.Light, id=0x7f10008fcom.example.xml_test:style/Base.V14.Theme.MaterialComponents.Light, id=0x7f100090com.example.xml_test:style/Base.V14.Theme.MaterialComponents.Light.Bridge, id=0x7f100134com.example.xml_test:style/Platform.MaterialComponents.Light, id=0x7f10020acom.example.xml_test:style/Theme.AppCompat.Light, id=0x7f100050com.example.xml_test:style/Base.Theme.AppCompat.Light, id=0x7f1000b3com.example.xml_test:style/Base.V28.Theme.AppCompat.Light, id=0x7f1000b0com.example.xml_test:style/Base.V26.Theme.AppCompat.Light, id=0x7f1000aacom.example.xml_test:style/Base.V23.Theme.AppCompat.Light, id=0x7f1000a8com.example.xml_test:style/Base.V22.Theme.AppCompat.Light, id=0x7f10009dcom.example.xml_test:style/Base.V21.Theme.AppCompat.Light, id=0x7f1000b6com.example.xml_test:style/Base.V7.Theme.AppCompat.Light, id=0x7f100131com.example.xml_test:style/Platform.AppCompat.Light, id=0x7f10013ccom.example.xml_test:style/Platform.V25.AppCompat.Light, id=0x1030241android:style/Theme.Material.Light.NoActionBar, id=0x1030237android:style/Theme.Material.Light, id=0x103000candroid:style/Theme.Light, id=0x1030005android:style/Theme], Themes=[com.example.xml_test:style/Theme.Xml_test, forced, com.example.xml_test:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3738)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3878)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2401)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8128)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:946)
Caused by: android.view.InflateException: Binary XML file line #108 in com.example.xml_test:layout/main: Can't convert value at index 7 to dimension: type=0x3, theme={InheritanceMap=[id=0x7f100262com.example.xml_test:style/Theme.Xml_test, id=0x7f10023acom.example.xml_test:style/Theme.MaterialComponents.DayNight.DarkActionBar, id=0x7f100253com.example.xml_test:style/Theme.MaterialComponents.Light.DarkActionBar, id=0x7f10006acom.example.xml_test:style/Base.Theme.MaterialComponents.Light.DarkActionBar, id=0x7f100068com.example.xml_test:style/Base.Theme.MaterialComponents.Light, id=0x7f1000a1com.example.xml_test:style/Base.V21.Theme.MaterialComponents.Light, id=0x7f10008fcom.example.xml_test:style/Base.V14.Theme.MaterialComponents.Light, id=0x7f100090com.example.xml_test:style/Base.V14.Theme.MaterialComponents.Light.Bridge, id=0x7f100134com.example.xml_test:style/Platform.MaterialComponents.Light, id=0x7f10020acom.example.xml_test:style/Theme.AppCompat.Light, id=0x7f100050com.example.xml_test:style/Base.Theme.AppCompat.Light, id=0x7f1000b3com.example.xml_test:style/Base.V28.Theme.AppCompat.Light, id=0x7f1000b0com.example.xml_test:style/Base.V26.Theme.AppCompat.Light, id=0x7f1000aacom.example.xml_test:style/Base.V23.Theme.AppCompat.Light, id=0x7f1000a8com.example.xml_test:style/Base.V22.Theme.AppCompat.Light, id=0x7f10009dcom.example.xml_test:style/Base.V21.Theme.AppCompat.Light, id=0x7f1000b6com.example.xml_test:style/Base.V7.Theme.AppCompat.Light, id=0x7f100131com.example.xml_test:style/Platform.AppCompat.Light, id=0x7f10013ccom.example.xml_test:style/Platform.V25.AppCompat.Light, id=0x1030241android:style/Theme.Material.Light.NoActionBar, id=0x1030237android:style/Theme.Material.Light, id=0x103000candroid:style/Theme.Light, id=0x1030005android:style/Theme], Themes=[com.example.xml_test:style/Theme.Xml_test, forced, com.example.xml_test:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 7 to dimension: type=0x3, theme={InheritanceMap=[id=0x7f100262com.example.xml_test:style/Theme.Xml_test, id=0x7f10023acom.example.xml_test:style/Theme.MaterialComponents.DayNight.DarkActionBar, id=0x7f100253com.example.xml_test:style/Theme.MaterialComponents.Light.DarkActionBar, id=0x7f10006acom.example.xml_test:style/Base.Theme.MaterialComponents.Light.DarkActionBar, id=0x7f100068com.example.xml_test:style/Base.Theme.MaterialComponents.Light, id=0x7f1000a1com.example.xml_test:style/Base.V21.Theme.MaterialComponents.Light, id=0x7f10008fcom.example.xml_test:style/Base.V14.Theme.MaterialComponents.Light, id=0x7f100090com.example.xml_test:style/Base.V14.Theme.MaterialComponents.Light.Bridge, id=0x7f100134com.example.xml_test:style/Platform.MaterialComponents.Light, id=0x7f10020acom.example.xml_test:style/Theme.AppCompat.Light, id=0x7f100050com.example.xml_test:style/Base.Theme.AppCompat.Light, id=0x7f1000b3com.example.xml_test:style/Base.V28.Theme.AppCompat.Light, id=0x7f1000b0com.example.xml_test:style/Base.V26.Theme.AppCompat.Light, id=0x7f1000aacom.example.xml_test:style/Base.V23.Theme.AppCompat.Light, id=0x7f1000a8com.example.xml_test:style/Base.V22.Theme.AppCompat.Light, id=0x7f10009dcom.example.xml_test:style/Base.V21.Theme.AppCompat.Light, id=0x7f1000b6com.example.xml_test:style/Base.V7.Theme.AppCompat.Light, id=0x7f100131com.example.xml_test:style/Platform.AppCompat.Light, id=0x7f10013ccom.example.xml_test:style/Platform.V25.AppCompat.Light, id=0x1030241android:style/Theme.Material.Light.NoActionBar, id=0x1030237android:style/Theme.Material.Light, id=0x103000candroid:style/Theme.Light, id=0x1030005android:style/Theme], Themes=[com.example.xml_test:style/Theme.Xml_test, forced, com.example.xml_test:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:795)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:8476)
at androidx.constraintlayout.widget.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2598)
at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1823)
at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:481)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1137)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1096)
at android.view.LayoutInflater.inflate(LayoutInflater.java:694)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at com.example.xml_test.databinding.MainBinding.inflate(MainBinding.java:85)
at com.example.xml_test.databinding.MainBinding.inflate(MainBinding.java:79)
at com.example.xml_test.main_activity.onCreate(main_activity.kt:24)
at android.app.Activity.performCreate(Activity.java:8591)
at android.app.Activity.performCreate(Activity.java:8569)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1455)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3720)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3878)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2401)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8128)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:946)
MANIFEST:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Xml_test"
tools:targetApi="31"
>
<activity
android:name=".main_activity"
android:exported="true"
android:theme="@style/Theme.Xml_test"
>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main_activity"
>
<EditText
android:id="@+id/service_cost"
android:layout_width="160dp"
android:layout_height="100dp"
android:layout_marginTop="@string/top_margin_main"
android:layout_marginStart="@string/start_margin_main"
android:autofillHints="@string/service_cost"
android:hint="@string/service_cost"
android:inputType="numberDecimal"
android:labelFor="@string/service_cost"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/how_was_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@string/start_margin_main"
android:text="@string/how_was_service"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/service_cost"
/>
<RadioGroup
android:id="@+id/tip_options"
android:checkedButton="@id/fifteen_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@string/start_margin_main"
app:layout_constraintTop_toBottomOf="@id/how_was_service"
app:layout_constraintStart_toStartOf="parent"
>
<RadioButton
android:id="@+id/fifteen_percent"
android:text="@string/fifteen_percent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/tip_options"
app:layout_constraintRight_toRightOf="@id/tip_options"
app:layout_constraintTop_toBottomOf="@id/how_was_service"
app:layout_constraintBottom_toTopOf="@id/eighteen_percent"
>
</RadioButton>
<RadioButton
android:id="@+id/eighteen_percent"
android:text="@string/eighteen_percent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/tip_options"
app:layout_constraintRight_toRightOf="@id/tip_options"
app:layout_constraintTop_toBottomOf="@id/fifteen_percent"
app:layout_constraintBottom_toTopOf="@id/twenty_percent"
>
</RadioButton>
<RadioButton
android:id="@+id/twenty_percent"
android:text="@string/twenty_percent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/tip_options"
app:layout_constraintRight_toRightOf="@id/tip_options"
app:layout_constraintTop_toBottomOf="@id/eighteen_percent"
app:layout_constraintBottom_toTopOf="@id/calculateButton"
>
</RadioButton>
</RadioGroup>
<androidx.appcompat.widget.SwitchCompat
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/round_up"
android:layout_marginStart="@string/start_margin_main"
android:id="@+id/round_up_switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tip_options"
app:layout_constraintBottom_toTopOf="@id/calculateButton"
/>
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginStart="@string/start_margin_main"
android:padding="20dp"
android:text="@string/calculate_tip"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/round_up_switch">
</Button>
<TextView
android:id="@+id/tip_final"
android:text="@string/tip_amount"
tools:text="Tip Amount: $10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@string/start_margin_main"
app:layout_constraintTop_toBottomOf="@id/calculateButton"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MAIN ACTIVITY:
package com.example.xml_test
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.xml_test.databinding.MainBinding
import java.text.NumberFormat
class main_activity : AppCompatActivity(){
private lateinit var binding: MainBinding
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
binding = MainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.calculateButton.setOnClickListener{calculateTip()}
}
fun calculateTip(){
val cost = binding.serviceCost.text.toString().toDouble()
val selectedButton = binding.tipOptions.checkedRadioButtonId
val tipPercent = when (selectedButton){
R.id.twenty_percent -> 0.20
R.id.eighteen_percent -> 0.18
else -> 0.15
}
var tip = cost * tipPercent
val roundUp = binding.roundUpSwitch.isChecked
if(roundUp){
tip = kotlin.math.ceil(tip)
}
val formattedTip = NumberFormat.getCurrencyInstance().format(tip)
binding.tipFinal.text = getString(R.string.tip_amount,formattedTip)
}
}
I assumed that the error had something to do with the value taken from the EditText because I saw something mentioning that there was a failure in converting a value. However, when I looked at line 108 reported in the error message, it was empty, so I'm not really sure where the error actually is. I am also unsure of what the message means by index 7 of the view. Additionally, when I compare my code to the working sample template in android studio I don't notice any differences.
Since you're new I'll show you how to read the stacktrace - it's very useful for tracking down errors, and it'll often tell you exactly what's going wrong!
The first line is the error that actually caused your app to crash
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.xml_test/com.example.xml_test.main_activity}:
android.view.InflateException: Binary XML file line #108 in
com.example.xml_test:layout/main: Can't convert value at index 7 to dimension...
That's often all you need, if the error message is helpful enough (and in this case, it is!). If you're not sure though, the lines below show you the sequence of calls that led to the call that crashed the app:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3738)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3878)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
This isn't the best example, since it's all internal system stuff that's starting up the Activity
- nothing you generally need to know about. But the calls are in reverse order - the bottom method called the method above it, which called the method above that, etc, until you hit the one that caused the error. This way, you can work out where a crash is happening and possibly guess why, just by looking at the steps being taken.
Below that is another error - that's the one that caused the error that crashed your app. Basically errors and exceptions "bubble up", and if a method calls something that throws an error, it might catch and rethrow an error to give more info about what went wrong. So the top section is an error and the steps that led up to it, the next section is where that error came from and the steps that led up to that, etc.
Caused by: android.view.InflateException: Binary XML file line #108 in
com.example.xml_test:layout/main: Can't convert value at index 7 to dimension
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 7 to dimension
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:795)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:8476)
at androidx.constraintlayout.widget.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2598)
at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1823)
at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:481)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1137)
So the final error was caused by an InflateException
, which is related to a failed attempt to inflate an XML file (i.e. create the hierarchy of layout View
objects from the information in the XML file).
That error was caused specifically by an UnsupportedOperationException
, in this case being unable to convert some value to a dimension. (You can see how the error message here was repeated by all the other exception messages, so we didn't really need to read this far down for that info.)
Following that were all the calls that led up to that exception - you can see they relate to ConstraintLayout
trying to measure some things and get some values. Other errors might be more useful, especially if it's your own code and you know what all these methods are and why they're being called!
And that's basically how you read a stacktrace. Always super useful, and definitely post them if you're looking for help!
As far as the specific error here goes, the message is definitely useful:
Binary XML file line #108 in
com.example.xml_test:layout/main: Can't convert value at index 7 to dimension
So at line 108 of layout/main.xml you have some value that's not a valid dimension:
A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure, such as 10px, 2in, or 5sp
I'm assuming the error is this line (the number doesn't match up exactly with what you posted):
android:layout_marginStart="@string/start_margin_main"
A string value for a dimension like this is very suspicious - offhand I'm not sure you can even put something like "16dp"
in a string resource and have it work. The correct way to do it is with a dimen
resource, which is explained in that link up there. You create a file in res/values
(call it dimens.xml
if you like) and then add a dimen
resource like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="start_margin_main">16dp</dimen>
</resources>
and then you reference it in your XML:
android:layout_marginStart="@dimen/start_margin_main"
You've used that string resource in a few places, so that's what you need to replace to fix it