令牌上的语法错误

时间:2014-03-12 23:06:47

标签: animation syntax imageview token

我正在尝试让图像从左到右,从右到左移动。这是我的代码:

    package com.example.;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class PlayActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);
}
ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);

  TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
    0.0f, 0.0f);
  animation.setDuration(5000);
  animation.setRepeatCount(5);
  animation.setRepeatMode(2);
  animation.setFillAfter(true);
  imageView2.startAnimation(animation);

然而,在最后5行,它给了我这些错误:

Multiple markers at this line
    - Syntax error on token(s), misplaced 
     construct(s)
    - Syntax error on token "5000", delete this 
     token
Multiple markers at this line
    - Syntax error on token "5", delete this 
     token
    - Syntax error on token(s), misplaced 
Multiple markers at this line
    - Syntax error on token(s), misplaced 
     construct(s)
    - Syntax error on token "2", delete this 
     token
Multiple markers at this line
    - Syntax error on token "true", delete this 
     token
    - Syntax error on token(s), misplaced 
Multiple markers at this line
    - Syntax error on token(s), misplaced construct(s)
    - Syntax error on token "animation", VariableDeclaratorId expected after 
     this token

有人能告诉我要解决什么吗?除了那5行之外,一切似乎都没问题

1 个答案:

答案 0 :(得分:0)

此代码现在适用于我

    package com.example.;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.animation.TranslateAnimation;
    import android.widget.ImageView;
    public class PlayActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_play);
        }
        ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);

        TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
                0.0f, 0.0f);
        protected TranslateAnimation setanimation(TranslateAnimation animation)
        {
            animation.setDuration(5000);
            animation.setRepeatCount(5);
            animation.setRepeatMode(2);
            animation.setFillAfter(true);
            imageView2.startAnimation(animation);
            return animation;
        }

}

问题在于您尝试在函数体外部设置动画属性。所以我通过创建函数protected TranslateAnimation setanimation(TranslateAnimation animation)来修复它,以保存您设置动画属性的代码,现在它编译并运行正常但我不知道您的预期输出是什么。

希望这有帮助