I have tried to use Gif image into my SplashScreen. When I run it I get the following error and an app is crashed.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Please help me to fix it. Your answer is more appreciated.
Here is my code:
public class SplashScreen extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MYGIFView(context));
context=this;
}
public class MYGIFView extends View {
Movie movie, movie1;
InputStream is = null, is1 = null;
long moviestart;
public MYGIFView(Context con) {
super(con); // ERROR IN THIS LINE
con=context;
is = con.getResources().openRawResource(R.raw.splash_screen);
movie = Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now = android.os.SystemClock.uptimeMillis();
System.out.println("now=" + now);
if (moviestart == 0) { // first time
moviestart = now;
}
System.out.println("\tmoviestart=" + moviestart);
int relTime = (int) ((now - moviestart) % movie.duration());
System.out.println("time=" + relTime + "\treltime=" + movie.duration());
movie.setTime(relTime);
movie.draw(canvas, this.getWidth() / 2 - 20, this.getHeight() / 2 - 40);
this.invalidate();
}
}
}