I’ve created OpenGLRenderer.java class and placed some code in it, shows no errors. Then I placed this code for creating a view in RoomFragment.java fragment:
public class RoomFragment extends Fragment {
/** Called when the fragment is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new OpenGLRenderer());
setContentView(this);
}}
But I’m getting
The constructor GLSurfaceView(RoomFragment) is undefined
and
The method setContentView(RoomFragment) is undefined for the type RoomFragment
errors. Aren’t those methods implemented in header? I’m guessing the reason for that is that this is not an activity, but a fragment which is active only on button click of a previous fragment (which is active on a main activity menu selection).
How do I go about this? How do I create GLSurfaceView in fragments layer?
The GLSurfaceView needs to be related to an
Activityby giving it aContext.Fragmentdoes not extend fromActivityand from the looks of it, you’re trying to create aGLSurfaceViewright from inside it.Also,
setContentViewmust be called from anActivity, essentially what you’re saying is ‘I want this Activity to be displayed in the way**Viewis telling it to’. Therefore you have to call the method from the Activity itself.Try either putting the
GLSurfaceViewinside theActivityfrom where you call theFragment, OR use theFragment‘sgetActivity()method to retrieve theActivityit’s bound to.The second solution would end up looking like this:
I’m not sure if that’s what you’re looking for, let me know if it works!