17

I'm having trouble setting the background for my button. I tried setting it to

android:background="@drawable/mybutton_background" 

but that caused an error, saying "String types not allowed" referring to "@drawable/mybutton_background". It also caused another error causing me to lose my R.java file and giving me the "R cannot be resolved into a variable error". I just gave up on the button and deleted the background, but the error remained after it was deleted. Well, that error is now gone and I now get a message that says Error parsing XML: duplicate attributes for one line of code, but once I delete that line, the error message moves on to another line! It's like Eclipse won't update to realize I deleted that line of code. I checked for updates and cleaned my project countless times, but I'm still getting the same error. Does anyone know what's up with my project?

My error log:

activity_main.xml: Paint.setShadowLayer is not supported.
activity_main.xml: Failed to convert @drawable/ into a drawable
activity_main.xml: Failed to convert android:background=" into a drawable

activity_main:

<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"
    android:background="android:background=&quot;"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ball01" />

    <Button
        android:background="@drawable/"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_marginBottom="17dp"
        android:background="@drawable/mybutton_background"  <----- where I get the error
        android:focusableInTouchMode="true"
        android:text="Enlighten me!"
        android:textColor="#3f0f7f"
        android:textSize="32sp"
        android:textStyle="bold|italic"
        android:typeface="serif"
        android:visibility="visible" />

    <LinearLayout
        android:layout_width="match_parent"0        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:weightSum="1" >

        <View
            android:id="@+id/view1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:gravity="center_horizontal"
            android:shadowColor="@android:color/white"
            android:shadowRadius="10"
            android:textSize="32sp" />

        <View
            android:id="@+id/view2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2" />

    </LinearLayout>

</RelativeLayout>

MainActivity.java:

package com.example.crystalball;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


    private CrystalBall mCrystalBall = new CrystalBall();
    private TextView mAnswerLabel;
    private Button mGetAnswerButton;
    private ImageView mCrystallBallImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAnswerLabel = (TextView) findViewById(R.id.textView1);
        mGetAnswerButton = (Button) findViewById(R.id.button1);
        mCrystallBallImage = (ImageView) findViewById(R.id.imageView1);

        mGetAnswerButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String answer = mCrystalBall.getAnAnswer();

                mAnswerLabel.setText(answer);

                animateCrystalBall();
                animateAnswer();
            }
        });
    }

    public void animateCrystalBall() {
        mCrystallBallImage.setImageResource(R.drawable.ball_animation);
        AnimationDrawable ballAnimation = (AnimationDrawable) mCrystallBallImage.getDrawable();
        if (ballAnimation.isRunning()) {
            ballAnimation.stop();
        }
        ballAnimation.start();
    }

    private void animateAnswer() {
        AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
        fadeInAnimation.setDuration(1500);
        fadeInAnimation.setFillAfter(true);

        mAnswerLabel.setAnimation(fadeInAnimation);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Thanks for anything you can contribute!

3
  • Check out this line in your RelativeLayout android:background="android:background=&quot;" this is also WRONG Commented Mar 24, 2014 at 14:19
  • Thanks! I got an error message there too. And that might be what's screwing it up. What should it be? Commented Mar 24, 2014 at 14:25
  • Nothing at all, if you don't want a global layout background. Or, android:background="@drawable/your_drawable_here" if you want it. Commented Mar 24, 2014 at 14:32

6 Answers 6

49

In my case this error was due to duplicate xmlns attributes inside layout. If you have something like this:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="person"
            type="com.abc.PersonEntity" />
    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
...

Remove the xmlns attribute from the second layout:

    <LinearLayout 
...
Sign up to request clarification or add additional context in comments.

1 Comment

This also happens when you have extra attributes like layout_width or layout_height on the <layout view
4

From the parent RelativeLayout XMl android:background="android:background=&quot;" or update it with a drawable or with color as android:background="#FFFF00"

<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"
    android:background="android:background=&quot;" <---remove this or update with correct drawable
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

In this below Button XML, you have added android:background attribute 2 times...Which causing the problem.

<Button
    android:background="@drawable/"  <-----first time
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_marginBottom="17dp"
    android:background="@drawable/mybutton_background"  <-----second time
    android:focusableInTouchMode="true"
    android:text="Enlighten me!"
    android:textColor="#3f0f7f"
    android:textSize="32sp"
    android:textStyle="bold|italic"
    android:typeface="serif"
    android:visibility="visible" />

Now, remove the first one...your problem will be solved.

2 Comments

Sorry, I was playing with it and tried to add a background twice then take it away to see if that would remove the error. I deleted the first background, and the message remains at android:background=:drawable/my_button" after I cleaned the project.
After I deleted the background. The error moved to android:focusableInTouchMode="true" beneath it.
1

You have the attribute background repeated

        android:background="@drawable/"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_marginBottom="17dp"
        android:background="@drawable/mybutton_background"  <----- where I get the error

Comments

1

android:background="android:background=&quot;" <--quot; remove code here

Look at this line in your parent relative layout. Just remove the background from here. Because of this line it causes "String types not allowed".

And second problem in below Button XML, you have added android:background 2 times...Which causing "Error parsing XML: duplicate attributes for one line of code". So just remove one background.

enter code here

<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"`enter code here`
    android:background="android:background=&quot;" <--Remove this line
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ball01" />

    <Button
        android:background="@drawable/"<-- Remove this line
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_marginBottom="17dp"
        android:background="@drawable/mybutton_background"  <----- where I get the error
        android:focusableInTouchMode="true"
        android:text="Enlighten me!"
        android:textColor="#3f0f7f"
        android:textSize="32sp"
        android:textStyle="bold|italic"
        android:typeface="serif"
        android:visibility="visible" />

    <LinearLayout
        android:layout_width="match_parent"0 <--remove this 0 from here.       
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:weightSum="1" >

        <View
            android:id="@+id/view1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:gravity="center_horizontal"
            android:shadowColor="@android:color/white"
            android:shadowRadius="10"
            android:textSize="32sp" />

        <View
            android:id="@+id/view2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2" />

    </LinearLayout>

</RelativeLayout>

Comments

1

In my case error was due to 2 id's android:id="@+id/layout" ( id) and android:id="@+id/parent" (Linear layout id)

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout"
android:background="@color/plain_background">

<LinearLayout
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>

Comments

0

In my case xmlns:android = "http://schemas.android.com/apk/res/android" was missing in one of the manifests. In integrated debug manifest file this attribute is shown twice. Adding this attribute to each amnifest (even an empty one) solved the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.