我正在按照Sams Teach Yourself Android Applications工作簿进行操作,并按照本书中的说明为QuizSplashActivity课程填写了以下内容。但是,我在粗体 * *的代码中出现错误,其中显示AnimationListener()
:
package com.androidbook.triviaquiz;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class QuizSplashActivity extends QuizActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TextView logo1 = (TextView) findViewById(R.id.textViewTopTitle);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);
TextView logo2 = (TextView) findViewById(R.id.textViewBottomTitle);
Animation fade3 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
logo2.startAnimation(fade3);
Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
LayoutAnimationController controller = new LayoutAnimationController(spinin);
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
for (int i = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i);
row.setLayoutAnimation(controller);
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
fade2.setAnimationListener(new **AnimationListener()** {
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
});
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// stop the animation
TextView logo1 = (TextView) findViewById(R.id.textViewTopTitle);
logo1.clearAnimation();
TextView logo2 = (TextView) findViewById(R.id.textViewBottomTitle);
logo2.clearAnimation();
// ... stop other animations
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
for (int i = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i);
row.clearAnimation();
}
}
}
错误是:
&#34;
The type new Animation.AnimationListener(){} must implement the inherited abstract method Animation.AnimationListener.onAnimationStart(Animation
)&#34;
这本书没有提到任何关于这一点,只是想知道是否有人可以提供帮助。
提前致谢。
摩西
答案 0 :(得分:2)
AnimationListener
是一个界面。这意味着当您将其放入代码中时,您必须实现所有方法。
如API所示,有三种方法:
onAnimationStart(Animation a)
onAnimationEnd(Animation a)
onAnimationRepeat(Animation a)
您只实施了一个:onAnimationEnd
。
你必须实施其他人,即使你什么也不做。
以下是您修改后的代码:
fade2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
public void onAnimationStart(Animation a) { }
public void onAnimationRepeat(Animation a) { }
});
请注意班级中的新方法。
我希望这有帮助!