Rasik
Rasik

Reputation: 2420

java.lang.classcast exception error in android splashscreen

I have a splashscreen where i am geting error........

My class is :

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;  
}      

}

i got error..

java.lang.RuntimeException: Unable to startactivity
ComponentInfo{com.example.aajakobazzar/com.example.aajakobazzar.Splash}:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable

what can i do?????

Upvotes: 0

Views: 175

Answers (3)

AndyN
AndyN

Reputation: 1754

As your R.drawable.splash is just an image and not any xml for animation, you need to make the object of BitmapDrawable instead of AnimationDrawable. That is the actual reason for your error i guess.

try

  `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();                  
            }              
        });  `

Upvotes: 0

noni
noni

Reputation: 2947

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

Your problem is there, you are casting a BitmapDrawable to a AnimationDrawable. That causes a ClassCastException.

You should declare frameAnimation as a BitmapDrawable

Upvotes: 0

Piyush
Piyush

Reputation: 18923

Change

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

to

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

Also Change:

splashImageView.setBackgroundResource(R.drawable.splash); 

to

splashImageView.setImageResource(R.drawable.splash);   

Upvotes: 1

Related Questions