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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22611927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:15:08  来源:igfitidea点击:

Error parsing XML: duplicate attributes?

android

提问by user3442610

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

但这导致了一个错误,说"String types not allowed"是指"@drawable/mybutton_background"。它还导致另一个错误,导致我丢失R.java文件并给我"R cannot be resolved into a variable error". 我只是放弃了按钮并删除了背景,但删除后错误仍然存​​在。好吧,那个错误现在消失了,我现在收到一条消息,上面写着 Error parsing XML: duplicate attributes for one line of code,但是一旦我删除了该行,错误消息就会移到另一行!这就像 Eclipse 不会更新以意识到我删除了那行代码。我无数次检查更新并清理我的项目,但我仍然遇到同样的错误。有谁知道我的项目是怎么回事?

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:

主活动.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!

感谢您的贡献!

回答by live-love

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

在我的例子中,这个错误是由于布局中重复的 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"
...

Remove the xmlns attribute from the second layout:

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

    <LinearLayout 
...

回答by Hamid Shatu

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

从父RelativeLayoutXMLandroid:background="android:background=&quot;"或使用可绘制或颜色更新它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 ButtonXML, you have added android:backgroundattribute 2 times...Which causing the problem.

在下面的ButtonXML 中,您添加了android:background2 次属性......这导致了问题。

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

现在,删除第一个……您的问题将得到解决。

回答by Guillermo Merino

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

回答by purisumit

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

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:background2 times...Which causing "Error parsing XML: duplicate attributes for one line of code".So just remove one background.

下面的 Button XML 中的第二个问题,你已经添加了android:background2 次......这导致了"Error parsing XML: duplicate attributes for one line of code".所以只需删除一个背景。

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>

回答by Zon

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.

在我的情况下xmlns:android = "http://schemas.android.com/apk/res/android",其中一个清单中缺少。在集成调试清单文件中,此属性显示两次。将此属性添加到每个 amnifest(甚至是空的)解决了这个问题。

回答by Praful C

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

在我的例子中,错误是由于 2 id 的 android:id="@+id/layout" ( id) 和 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>