我有一个启动画面,我收到错误........
我的课程是:
package com.example.aajakobazzar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
public class Splash extends Activity {
private Thread SplashThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final ImageView splashImageView =
(ImageView) findViewById(R.id.SplashImageView);
splashImageView.setBackgroundResource(R.drawable.splash);
final AnimationDrawable frameAnimation =
(AnimationDrawable)splashImageView.getBackground();
splashImageView.post(new Runnable(){
@Override
public void run() {
frameAnimation.start();
}
});
final Splash splashscreen = this;
// The thread to wait for splash screen events
SplashThread = new Thread(){
@Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(splashscreen, MainMenu.class);
startActivity(intent);
stop();
}
};
SplashThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(SplashThread){
SplashThread.notifyAll();
}
}
return true;
}
}
我收到了错误..
java.lang.RuntimeException: Unable to startactivity
ComponentInfo{com.example.aajakobazzar/com.example.aajakobazzar.Splash}:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
我该怎么办?????
答案 0 :(得分:1)
更改
final AnimationDrawable frameAnimation =
(AnimationDrawable)splashImageView.getBackground();
到
final AnimationDrawable frameAnimation =
(AnimationDrawable)splashImageView.getDrawable();
同样改变:
splashImageView.setBackgroundResource(R.drawable.splash);
到
splashImageView.setImageResource(R.drawable.splash);
答案 1 :(得分:0)
由于你的R.drawable.splash只是一个图像,而不是动画的任何xml,你需要创建BitmapDrawable的对象而不是AnimationDrawable。 这就是我猜错误的实际原因。
尝试
`final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
splashImageView.setImageBitmap(null);
splashImageView.setImageResource(R.drawable.splash);
final AnimationDrawable frameAnimation = (AnimationDrawable)splashImageView.getDrawable();
splashImageView.post(new Runnable(){
@Override
public void run() {
frameAnimation.start();
}
}); `
答案 2 :(得分:0)
final AnimationDrawable frameAnimation =
(AnimationDrawable)splashImageView.getBackground();
您的问题在那里,您正在向BitmapDrawable
投射AnimationDrawable
。这会导致ClassCastException。
您应该将frameAnimation声明为BitmapDrawable