我試圖做一個簡單的淡入淡出,通過從另一個類別調用它淡出我的活動中的動畫。其實我有一個活動和兩個簡單的課程。在我Activiy我打電話從第一類這樣的方法:從另一個類別開始動畫
public TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.hello);
Megegy me = new Megegy(txt,MainActivity.this);
me.animatika();
}
下面是這個Megegy類:
public class Megegy {
TextView txt;
Activity activity;
public Megegy(TextView txt,Activity activity){
this.activity = activity;
this.txt = txt;
}
Fadede fa = new Fadede(activity,txt);
public void animatika(){
fa.animati();
}
}
在這個類我稱之爲從包含其他類一個方法的動畫:
public class Fadede implements Animation.AnimationListener {
Animation animFadein;
Animation animFadeout;
public TextView txt;
Activity activity;
public Fadede(Activity activity, TextView txt) {
this.activity = activity;
this.txt = txt;
}
public void animati(){
animFadein = AnimationUtils.loadAnimation(activity, R.anim.fadein);
animFadein.setAnimationListener(this);
animFadeout = AnimationUtils.loadAnimation(activity, R.anim.fadeout);
animFadeout.setAnimationListener(this);
txt.startAnimation(animFadein);
}
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for fade in animation
if (animation == animFadein) {
}
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
而問題是,當我運行這個程序,我得到了 顯示java.lang.NullPointerException這條線:
animFadein = AnimationUtils.loadAnimation(activity, R.anim.fadein);
我不知道是什麼原因導致這個錯誤,但如果有人有一個請回復!
你爲什麼不把那行'Fadede FA =新淡出de(activity,txt);'在構造函數中? –