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

use*_*610 12 android

我在设置按钮的背景时遇到问题.我试过把它设置为

android:background="@drawable/mybutton_background" 
Run Code Online (Sandbox Code Playgroud)

但这引起了一个错误,说"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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

感谢您的贡献!

liv*_*ove 35

在我的情况下,此错误是由于布局内的重复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"
...
Run Code Online (Sandbox Code Playgroud)

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

    <LinearLayout 
...
Run Code Online (Sandbox Code Playgroud)

  • 当您在“&lt;layout”视图上有额外的属性(例如“layout_width”或“layout_height”)时,也会发生这种情况 (2认同)

Ham*_*atu 4

从父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" >
Run Code Online (Sandbox Code Playgroud)

在下面的ButtonXML 中,您添加了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" />
Run Code Online (Sandbox Code Playgroud)

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