Fatal Error: You must supply a layout_width attribute - android

I'm very very new to android programming and while trying to run this simple program I get the error: "Unfortunately your app stopped".
So I went and looked in the Logcat file and this is where, I think, the problem is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.example.tictactoe, PID: 3202
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.tictactoe/org.example.tictactoe.MainActivity}: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.
But looking at my files I don't see where It's missing.
This are the files .xml which I used:
activity_main.xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/lib/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
tools:context=".TicTacToeActivity">
<fragment
android:id="#+id/main_fragment"
android:name="org.example.tictactoe.MainFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:layout="#layout/fragment_main"/>
</FrameLayout>
fragment_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/lib/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/menu_background"
android:elevation="#dimen/elevation_high"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context=".TicTacToeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/menu_space"
android:text="#string/long_app_name"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="#dimen/menu_text_size"
/>
<Button
android:id="#+id/continue_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/continue_label"
/>
<Button
android:id="#+id/new_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/new_game_label"
/>
<Button
android:id="#+id/about_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/about_label"
/>
</LinearLayout>
menu_background:
<shape
xmlns:android="http://schemas.android.com/apk/lib/android"
android:shape="rectangle">
<stroke
android:width="#dimen/stroke_width"
android:color="#color/border_color"/>
<solid android:color="#color/field_color"/>
<corners android:radius="#dimen/corner_radius"/>
</shape>
I cannot find where the layout_width attribute is missing.

There are blank spaces in your layout. For example, in fragment_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/lib/android "
xmlns:tools="http://schemas.android.com/tools "
Use Find and Replace for " or in your layout.
UPDATE
You have another error in the following xml:
You can't use class="org.example.tictactoe.MainFragment". But you need to use android:name attribute. So, it should be:
<fragment
android:id="#+id/main_fragment"
android:name="org.example.tictactoe.MainFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:layout="#layout/fragment_main"/>
Found another problem in your xml code. Use xmlns:android="http://schemas.android.com/apk/res/android" instead of xmlns:android="http://schemas.android.com/apk/lib/android".

You have a space in "wrap_content " for layout_width and layout_height in MainFragment.

Remove android:layout_gravity="#layout/fragment_main" from activity_main.xml
android:layout_gravity requires specific parameters android:layout_gravity Reference Link
Fragments reference

Related

"unknown drawble resource" error

1) I'm seeing "unknown drawble resource" error when I use:"android:src="#drawble/party""
The complete code is shown below. What's the issue as my "party.jpg" file is under res/drawble folder?
2) I also see an error with "android:layout_alignRight="8dp"" What's the issue here?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="park.hannah.myapplication.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawble/party" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From. Hyunji"
android:padding="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Happy Tuesday!!"
android:layout_alignRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
YOU HAVE SYNTAX ERROR
#drawable/party instead of #drawble/party
use this
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/party" />
I also see an error with "android:layout_alignRight="8dp"" What's the
issue here?
you must align a view depend on another view by ID
suggestions
you can also use gravity right instead of android:layout_alignRight for not complex view layouts
try to use LinearLayout easy and comfort for not complex view layouts
rename your drawable from *.jpg to *.png

Cyclic include, not valid error while creating fragment android

I am creating a fragment to display a map. I have done someting like this:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Spinner
android:id="#+id/spr_place_type"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/spr_place_type"
android:text="#string/str_btn_find" />
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/spr_place_type"
class="com.google.android.gms.maps.SupportMapFragment"
/>
But in the graphical layout, I can see the following error:-
When I right click and choose the fragment layout I further see the following error:-
how can I resolve the following error.
You cannot put your main activity layout as a fragment inside your main activity layout. That's what the "cyclic include" error means.
You don't seem to have a fragment layout readily available. You can click on the "New Layout..." button to create a new one.

android text not showing in button

This happens very frequently to me when starting to work on android. The text within the button element is not showing fully. What can be wrong? I tried text direction, alignment, gravity but nothing works.
The code behind it:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="#string/latestButtonString"
android:background="#drawable/roundbutton" />
Please help and much appreciated.
Update: This is the roundbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
I had this problem, I solved it by chaning the xml for the button from using tools: to android: for the text attribute. Example below (last line is the line thats changed):
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
tools:text="App Settings" />
becomes...
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
android:text="App Settings" />
try to replace
android:text="#string/latestButtonString"
and put direct hardcode string here
as,
android:text="hello"
or
try to replace this line in roundbutton.xml:
<android:shape="rectangle" android:padding="10dp">
with
<android:shape="rectangle" android:padding="3dp">
Can you please check by removing android:background="#drawable/roundbutton" from your button attribute. If text shown fully, then you need to modify the #drawable/roundbutton" there may be you have set fixed width thus your text in button is not fully visible.
It seems that you have a paddingLeft on your Button. Can you check if this is present in your styles.xml?
If it is in your styles.xml, it is probably important for the theme of your app.
If you would just want to cancel it in that certain Button, just put
android:paddingLeft="0dp"
Faced a similar problem...following attribute made it work:
android:layout_weight="1"
please remove these line android:background="#drawable/roundbutton" but view not create like you if you want to create same view please try these code :
code
<LinearLayout 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"
android:gravity="center">
<LinearLayout
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="40dp"
android:padding="20dp"
android:background="#drawable/roundbutton">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="latestButtonString"
android:textColor="#000000"
android:textSize="18sp"/>
</LinearLayout>

Android XML error after updating eclipse

After recently updating eclipse i keep getting an error saying "Attribute name "android:tools" associated with an element type "RelativeLayout" must be followed by the ' = ' character.
It says it on line 9 but there isn't any android:tool there. I've tried cleaning and restarting eclipse and still no solution. I never changed any part of the file, and the funny thing is all my other activities are the same and don't have any errors. Any help would be appreciated thanks.
Edit... I fixed the problem by deleting the file and creating a new one. File must have been corrupt or something. Thanks for the input everyone, sorry for the run confusion.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/backtext"
tools:context=".Brakes"
android:screenOrientation="portrait" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/brakes1"
android:textColor="#color/textviews"
android:textSize="15sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:text="#string/brakes2"
android:textColor="#color/textviews"
android:textSize="15sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:text="#string/brakes3"
android:textColor="#color/textviews"
android:textSize="15sp"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView4"
android:text="#string/brakes4"
android:textColor="#color/textviews"
android:textSize="15sp"/>
</RelativeLayout>
</ScrollView>
Try moving this line:
xmlns:tools="http://schemas.android.com/tools"
to your ScrollView instead of the RelativeLayout where it is now.
it looks like your RelativeLayout used to be your root view, and you added the ScrollView at some point later.
you are missing xmlns:tools="http://schemas.android.com/tools" in your root parent,please include it.
Remove xmlns:android="http://schemas.android.com/apk/res/android" from your RelativeLayout. There should be only one of those at the layout root element.

This RelativeLayout layout or its LinearLayout parent is useless

I'm developing an Android 3.1. and above application.
On one activity I generate its layout dynamically. I'm using formdetail.xml as contentview:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.formdetail);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
currentFormId = extras.getString("FormId");
}
}
formdetail.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
downloadFormsButton and formErrorMsg must be always in form.
But using that xml I get this warning:
This RelativeLayout layout or its LinearLayout parent is useless
I need linearLayout to add TableLayouts programmatically.
How can I solve this warning?
Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" />
</RelativeLayout>
You either ignore it or right click your project. Click Build Path->Configure Build Path.... Under Android Lint Preferences look for UselessParent and set it's severity to ignore or click Ignore All.
EDIT:
I take that last part back. I think Ignore All will wipe all Lint warnings and errors.
I was also having the same issue with my app layout.
I don't know how and it is right way or not, but this get resolved after setting background attribute to both layouts.
Now there is no warning and working perfect. Hope it will also work for you.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/bg"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg2"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/img_BookImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:contentDescription="#string/India"
android:src="#drawable/india" />
</FrameLayout>
</FrameLayout>
That is just an warning from lint. Lint doesn't know you would later append views to this layout and it will warn you that you added an unnecessary extra view(increasing your layout depth).
You should just ignore the warning(If it really bothers you here is a link on how to remove warnings from lint).

Categories

Resources