0

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();
        }
    }
}
Parama Sudha
  • 2,583
  • 3
  • 29
  • 48

1 Answers1

1

Don't

setContentView(new MYGIFView(context));
context=this;

Do

setContentView(new MYGIFView(SplashScreen.this));

Edit

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

context is not passing

NullPointerException is thrown when an application attempts to use an object reference, having the null value.

You can try with

 context=this;
 setContentView(new MYGIFView(context));

Missing

setContentView(R.layout.Your_Activity_xml); // Missing
setContentView(new MYGIFView(this));

Try with this

public class SplashScreen extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MYGIFView(SplashScreen.this));

}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198