Reputation: 2113
I have an android project which is mostly the template app from android studio. Here is the activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/gameStartButton"
android:text="Start"
android:fontFamily="serif"
android:textSize="30sp"
android:textColor="@color/colorAccent"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="@string/mainMenuItemWidth"/>
</android.support.constraint.ConstraintLayout>
and here's the MainActivity.java
:
package com.example.saga.test;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
When I launch this app, it crashes with this traceback:
08-26 17:57:36.256 5868-5868/com.example.saga.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.saga.test, PID: 5868
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.saga.test/com.example.saga.test.MainActivity}: java.lang.RuntimeException: Binary XML file line #0: You must supply a layout_height attribute.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
As you can see, I have actually supplied a layout_height
attribute for both the constraint layout and the button, why then am I getting this error?
Upvotes: 0
Views: 206
Reputation: 9225
The app is crashed cause of android:layout_height="@string/mainMenuItemWidth"
, beacuse layout_height
is not accept string type value.
create a value in dimen
file with name of mainMenuItemWidth
Change android:layout_height="@string/mainMenuItemWidth"
to android:layout_height="@dimen/mainMenuItemWidth"
I hope its work for you.
Upvotes: -1
Reputation: 14173
Root cause: You are using a string for property android:layout_height
. The expected is an integer
or a dimen
value.
Solution: Create a new file in res/values
folder called dimens.xml
then add below section into it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="mainMenuItemWidth">20dp</dimen>
</resources>
Then in your xml file.
<Button
android:id="@+id/gameStartButton"
android:text="Start"
android:fontFamily="serif"
android:textSize="30sp"
android:textColor="@color/colorAccent"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="@dimen/mainMenuItemWidth"/>
Upvotes: 1
Reputation: 1995
You cannot use a string value for layout_height. Put the value inside dimens.xml and reference that value using
android:layout_height="@dimen/mainMenuItemWidth"
Upvotes: 1