【问题标题】:image cannot be resolved or is not a field图像无法解析或不是字段
【发布时间】:2014-02-23 01:07:50
【问题描述】:

我正在做一个 android 项目来使用 eclipse 创建动画。 在 MainActivity.java it 编译器显示 5 个 lint 错误,如下所示

-图像无法解析或不是字段 -activity_main 无法解析或不是字段 -translate 无法解析或不是字段 -rotate 无法解析或不是字段 -shape 无法解析或不是字段

这是我在 MainActivity.java 中的代码

    package ahmed103.appdemo;




    import android.R;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation.AnimationListener;
    import android.view.animation.*;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainActivity extends Activity implements AnimationListener {
ImageView image;
Animation animation1;
Animation animation2;
Button rotate, translate;
Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image = (ImageView) findViewById(R.id.image);
    rotate = (Button) findViewById(R.id.rotate);
    translate = (Button) findViewById(R.id.translate);
    image.setImageResource(R.drawable.shape);
    animation1 = AnimationUtils.loadAnimation(this, R.anim.rotate);
    animation2 = AnimationUtils.loadAnimation(this, R.anim.translate);

    userInputHandler();
}

private void userInputHandler() {
    rotate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            image.startAnimation(animation1);

        }
    });

    translate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            image.startAnimation(animation2);

        }
    });

}

@Override
public void onAnimationEnd(Animation animation) {
    image.setVisibility(View.VISIBLE);
}

@Override
public void onAnimationRepeat(Animation animation) {
    image.setVisibility(View.VISIBLE);
}

@Override
public void onAnimationStart(Animation animation) {
    image.setVisibility(View.VISIBLE);
}

}

下面是我的 activity_main.xml 文件

<html>
<LinearLayout 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="#DCDCDC"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="70dp"
    android:minHeight="150dp"
    android:minWidth="150dp" />

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</TableRow>

<Button
    android:id="@+id/rotate"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_weight="1"
    android:text="@string/rotate_text" />

<Button
    android:id="@+id/translate"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_weight="1"
    android:text="@string/translate_text" />

</LinearLayout>
</html>

【问题讨论】:

    标签: java xml eclipse android


    【解决方案1】:

    删除下面的导入

    import android.R;
    

    你也有

    <html> and </html>// should be removed from xml 
    

    进口

    import ahmed103.appdemo.R;
    

    并使用

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

    作为activity_main.xml 中的第一条语句

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 2014-03-27
      • 2014-01-05
      • 2016-05-20
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多