1

Hi guys i declared a custom GlSurfaceView in this way

public class Render extends GLSurfaceView implements GLSurfaceView.Renderer {

and in an other class i initializate it using:

render= (Render) view.findViewById(R.id.gl);

but at runtime the vm generate this exception

java.lang.ClassCastException: android.opengl.GLSurfaceView cannot be cast to com.pugtests.depugscompany.openglprove.Render

why ? Can anyone explain to me my mistake and help me to fix it.. sorry for my english and thanks to everyone.

shirowww
  • 533
  • 4
  • 18
P.Carlino
  • 661
  • 5
  • 21
  • Are you sure you're casting the instance you think you're casting? As in, you're casting something which isn't a `Render` at all. – Andy Turner Jan 09 '17 at 12:56
  • @Andy Turner what he tries to cast is the Class, whis is extended by his Renderer. – GAlexMES Jan 09 '17 at 12:58
  • @GAlexMES nope. It's not an instance of his class. If that were the case, the exception message would be `java.lang.ClassCastException: com.pugtests.depugscompany.openglprove.Render cannot be cast to ...`. (Except that that wouldn't occur, because the cast would be allowed). – Andy Turner Jan 09 '17 at 13:00
  • @Andy Turner, ah I guess I missunderstand you. You mean, that he is adding a GLSurfaceView to his view and is casting this to an Renderer? – GAlexMES Jan 09 '17 at 13:02
  • Not sure, but sounds like you add a GLSurfaceView instead of Render in your layout-xml. – MalaKa Jan 09 '17 at 13:03
  • Sorry but if render extends GlsurfaceView is it a GlSurfaceView and so a View so if the superclass is the same why there is a error?? – P.Carlino Jan 09 '17 at 13:44
  • There is an error because you are trying to cast a GLSurfaceView to Render. Yes, Render extends GLSurfaceView and therefore it is a View. But the other way round does not apply. Not every GLSurfaceView can be cast into Render. – MalaKa Jan 09 '17 at 14:40
  • Why sorry but i don't understand as well the reason..can you explain to me why? Thanks indeead – P.Carlino Jan 09 '17 at 14:43
  • This is a bit too much for the comments on SO. Search for Inheritance to find some explanations. An example is given [here](http://stackoverflow.com/questions/907360/explanation-of-classcastexception-in-java) and [here](https://www.tutorialspoint.com/java/java_inheritance.htm). – MalaKa Jan 09 '17 at 15:12

0 Answers0