Question

I am trying to instantiate an instance of a class that implements an interface. But somehow I am unable to do so. I do know that you can not instantiate an interface directly. But can do so if it is implemented in another class as I have done. What am I doing wrong.

Class in which implements and interface of another class

public class Renderer implements GLWallpaperService.Renderer {

    public Renderer(Context context) {

    }

}

My GLWallpaperService class:

public abstract class GLWallpaperService extends WallpaperService {  

     public static interface Renderer extends GLSurfaceView.Renderer {
     }
}

And the class in which I am receiving the error (Cannot instantiate the type GLWallpaperService.Render) The error is at the line renderer = new Renderer(glwallpaperservice);

public class LiveWallpaperService extends GLWallpaperService {

    class Engine extends GLEngine {
        long lastTouchDown;
        private int mSampleRate;
        private Visualizer mVisualizer;
        Context mContext;
        Renderer renderer;

        public Engine(GLWallpaperService glwallpaperservice) {
            super();
            Context context;
            this.lastTouchDown = 0;
            renderer = new Renderer(glwallpaperservice);
            setRenderer(renderer);
            setRenderMode(0);
            setTouchEventsEnabled(true);
        }
}
Was it helpful?

Solution

The line

renderer = new Renderer(glwallpaperservice);

is trying to use the Renderer interface, not the class. Interfaces cannot be instantiated, hence the error. Qualifying the class name should be enough:

renderer = new <your.package.name>.Renderer(glwallpaperservice);

As a suggestion anyway, it would be better if the interface and class names were different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top