我正在创建一个应用程序,我必须在启动画面中实现 alpha效果,当我加载图像时,它会给出null pointer exception
。启动动画时的基本问题。如果我删除启动动画然后我的动画根本没有开始。我真的卡住。任何帮助将不胜感激。我的代码如下:
public class SplashScreen extends Activity {
private Thread mSplashThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("hello");
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
Animation a1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
LinearLayout Ll=(LinearLayout)findViewById(R.id.mainLayoutheader);
System.out.println("hello1");
Ll.startAnimation(a1);
System.out.println("hello2");
// The thread to wait for splash screen events
mSplashThread = new Thread(){
@Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
// a.reset();
wait(6000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, Main.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
}
例外是L1.startAnimation(a1);
答案 0 :(得分:2)
我猜你忘了拨打setContentView
。你必须在调用findViewById
之前这样做[编辑:这里没有解决问题]
答案 1 :(得分:1)
Ll
为null或a1
为null,startAnimation抛出异常。完整的堆栈跟踪显示,哪一个扔了它。
此外:
R.id.mainLayoutheader
而不是R.id.mainLayoutHeader
。R.anim.alpha
是否正确。答案 2 :(得分:1)
您可以尝试计时器线程...使用此链接 http://thedevelopersinfo.wordpress.com/2009/10/18/scheduling-a-timer-task-to-run-repeatedly/
答案 3 :(得分:1)
问题是我显示图像的视图是空的 super.onCreate(savedInstanceState);
// Splash screen view
System.out.println("hello");
setContentView(R.layout.splash);
ImageView iv=(ImageView)findViewById(R.id.splashscreen12);
Animation scaleAnim = AnimationUtils.loadAnimation(this,R.anim.alpha);
iv.startAnimation(scaleAnim);
scaleAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
// Start new activity after some delay
TimerTask newActivity = new TimerTask() {
@Override
public void run() {
startActivity(new Intent(SplashScreeen.this,CallingActivity.class));
SplashScreeen.this.finish();
}
};
Timer t = new Timer(false);
t.schedule(newActivity, 2500);
}
@Override
public void onAnimationRepeat(Animation animation) {
// DO NOTHING
}
@Override
public void onAnimationStart(Animation animation) {
// DO NOTHING
}
});