Android初始屏幕中的java.lang.classcast异常错误



我有一个启动画面,我收到错误......

我的班级是:

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

我能做什么?????

更改

final AnimationDrawable frameAnimation =   
              (AnimationDrawable)splashImageView.getBackground();  

final AnimationDrawable frameAnimation =   
              (AnimationDrawable)splashImageView.getDrawable();  

还更改:

splashImageView.setBackgroundResource(R.drawable.splash); 

splashImageView.setImageResource(R.drawable.splash);   
final AnimationDrawable frameAnimation =   
                  (AnimationDrawable)splashImageView.getBackground(); 

你的问题就在那里,你正在向AnimationDrawableBitmapDrawable.这会导致 ClassCastException。

您应该将帧动画声明为位图可绘制对象

由于您的 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();                  
            }              
        });  `

相关内容

最新更新