解析XML时出错:重复属性?

时间:2014-03-24 14:16:02

标签: android

我无法设置按钮的背景。我尝试将其设置为

android:background="@drawable/mybutton_background" 

但是这导致了错误,说"String types not allowed"指的是"@drawable/mybutton_background"。它还导致了另一个错误,导致我丢失了R.java文件并向我发送了"R cannot be resolved into a variable error"。我只是放弃了按钮并删除了背景,但错误被删除后仍然存在。好吧,那个错误现在已经消失了,我现在收到一条消息,说明解析XML时出错:一行代码的重复属性,但是一旦删除该行,错误消息就会移到另一行!这就像Eclipse不会更新,意识到我删除了那行代码。我检查了更新并无数次清理了我的项目,但我仍然遇到同样的错误。有谁知道我的项目是什么?

我的错误日志:

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;
    }

}

感谢您的贡献!

6 个答案:

答案 0 :(得分:32)

在我的情况下,此错误是由于布局内的重复xmlns属性。如果您有这样的事情:

<?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"
...

从第二个布局中删除xmlns属性:

    <LinearLayout 
...

答案 1 :(得分:4)

来自父RelativeLayout XMl android:background="android:background=&quot;"或使用drawable或颜色更新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" >

在下面的Button XML中,您添加了android:background属性2次...导致问题的原因。

<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" />

现在,删除第一个......你的问题将会解决。

答案 2 :(得分:0)

您重复了属性背景

        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

答案 3 :(得分:0)

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

在父亲相对布局中查看此行。只需从此处删除背景。由于这一行,它导致&#34;不允许字符串类型&#34;。

在Button XML下面的第二个问题,您添加了android:background 2次...导致"Error parsing XML: duplicate attributes for one line of code".所以只删除一个背景。

在这里输入代码

<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>

答案 4 :(得分:0)

在我的案例中,其中一个清单中遗漏了gcloud。在集成的调试清单文件中,此属性显示两次。将此属性添加到每个amnifest(即使是空的)也解决了这个问题。

答案 5 :(得分:0)

在我的情况下,错误是由于2个ID     android:id =“ @ + id / layout”(id)     和     android:id =“ @ + id / parent”(线性布局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>