surfaceview + glsurfaceview + framelayout

I'm new at java and OpenGL.

I'm trying to get a camera preview screen with the ability to display 3d objects simultaneously. Having gone through the samples at the api demos, I thought combining the code for the the examples at the api demo would suffice. But somehow its not working. The forces me to shut down upon startup and the error is mentioned as null pointer exception. Could someone share with me where did I go wrong and how to proceed from there. How I did the combination for the code is as shown below:


<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="" 

import android.os.Bundle; 
import android.view.SurfaceView; 
import android.view.Window; 
public class MyOverView extends Activity { 
    protected void onCreate(Bundle savedInstanceState) { 
       // Hide the window title. 
       // camera view as the background 
       SurfaceView cameraView = (SurfaceView) findViewById(; 
       cameraView = new CameraView(this); 
       // visual of both cubes 
       GLSurfaceView cubesView = (GLSurfaceView) findViewById(; 
       cubesView = new GLSurfaceView(this); 
       cubesView.setRenderer(new CubeRenderer(false)); 
       // set view 

import android.content.Context; 
class GLSurfaceView extends android.opengl.GLSurfaceView { 
    public GLSurfaceView(Context context) { 


  • I didn't list the rest of the files as they are just copies of the api demos. The cameraView refers to the example and the CubeRenderer refers to the and example. Any help would be appreciated.

  • Sorry, didn't realize that the coding was out of place due to formatting mistakes.

Asked by: Melissa893 | Posted: 25-01-2022

Answer 1

the reason you are getting a null pointer exception when working with .xml is because ur actually creating new Views in your java code.. instead of using the ones from the .xml file to which you might have passed in properties(if u did pass in properties that is..).. the new View would obviously have a null value.. thus throwing a null pointer exception... for example --

cubesView = new GLSurfaceView(this);

is actually not needed in the code if you already created the View in the .xml file containing FrameLayout..

Answered by: Gianna186 | Posted: 26-02-2022

Answer 2

This is very simple actually...if you want to define your view in XML you just have to implement

Public GLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);

instead of GLSurfaceView(Context context)

That's the one that gets called automatically when the view is initialized from the XML. I had the same problem and that's how it was fixed.

Answered by: Anna846 | Posted: 26-02-2022

Answer 3

Found out how to solve it... via the java way... just use addContentView instead of using xml.... well at least its solved. :)

Answered by: Wilson705 | Posted: 26-02-2022

Answer 4

I actually did that here in this SO link which provides a complete implementation.

Answered by: Alissa915 | Posted: 26-02-2022

Similar questions

android - Using GLSurfaceView instead of SurfaceView with the camera preview app sample

I am trying to use a GLSurfaceView instead of a normal SurfaceView with a camera application. The app simply display the camera preview frame on a surfaceview object. It works, but since i want to add opengl es rendering on the camera preview frame, i started by extending a GLSurfaceView class instead of a SurfaceView. Just that. The application seems to work, but sometimes it mulfunction (the app don't exit anymore if i p...

android - Will GLSurfaceView render a bitmap faster than SurfaceView?

I'm rendering video from some external source as a series of bitmaps to a SurfaceView. Sometimes the rendering is not fast enough. I'm thinking of replacing SurfaceView with GLSurfaceView for bitmap rendering but then, since these are bitmaps and nothing like Open GL vectors I don't think the rendering will become faster. Shall I expect it to become faster? The change will require me brushing ...

opengl es - Difference between SurfaceView and GLSurfaceView in Android

Can anyone tell me what the basic difference is between SurfaceView and GLSurfaceView? When should I use SurfaceView, and when should I use GLSurfaceView? I read some already answered questions on Stack Overflow, but they did not satisfy my queries. Any help would be appreciated.

android - Cannot place GLSurfaceView over SurfaceView?

My understanding is you cannot overlay two surfaceviews in Android, based on this discussion:!topic/android-developers/COffLpanlz0 Further evidence it this does not work:

android - GLSurfaceView over SurfaceView

I need to have a GLSurfaceView over a Surface view. GLSurface view will have a Renderer and SurfaceView will be camera view. Following is my layout &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;RelativeLayout ...

surfaceview - Stop OnDrawFrame of Android GLSurfaceView

I am writing the application in which i am using GLSurfaceView to draw shapes on display.The issue is i want to pause the drawing or stop the drawing and resume it again.GLSurfaceView setRenderer is called only oncein lifetime of application.OnDrawFrame is overriden function of GLSurfaceView class os i cannot disable it.So i should i go about stopping/pausing the drawing and resume again.I am trying to draw as follows:...

surfaceview - Minimize Android GLSurfaceView lag

Following some other questions on Stack Overflow, I've read the guide to the internals of Android Surfaces, SurfaceViews, etc from here: That guide has given me a much improved understanding of how all the different pieces fit together on Android. It covers how eglSwapBuf...

opengl es - Android View vs SurfaceView vs GLSurfaceView for 2D drawing app with Zoomable User Interface

I plan to write a 2D drawing app with a zoomable user interface. With the app the user should be able to transform (translate and scale) drawn paths (and of course the UI). I assume that there will be up to 500 paths at the same time. Now, I am wondering which view to start off with (View, SurfaceView or GLSurfaceView ) in order to provide acceptable performance. I read a couple of posts [1-6] including the once on android...

android - Mixing Gstreamer SurfaceView and GLSurfaceView

I have an Android application that displays a video using gstreamer. It's similar to the tutorial mentioned here: Especially, it uses the GStreamerSurfaceView which extends SurfaceView. I want now to perform some treatments on the vide...

android - Overlaying a GLSurfaceView on top of another SurfaceView causes crash

I would like to overlay a modified SurfaceView with a GLSurfaceView with a transparent background, but adding the GLSurfaceView on top of the SurfaceView causes the application to crash. The modified SurfaceView displays a video obtained in realtime from a drone camera, the code is sampled directly from Parrot's SDK sample and it work perfectly. The GLSurfaceView is one that I have created, with a transparent backg...

android - How do I know when a GLSurfaceView is fully initialized from the Activity?

I have an Activity that contains several views. One of which is a GLSurfaceView. This GLSurfaceView displays pretty 3D effects based on what is selected in my Activity's other views (ListViews, EditTexts, etc). The issue I am having is that I don't know when my GLSurfaceView has been fully initialized from the Activity. I began by simply attempting to pass my graphics data to my GLSurfaceView in my Activity's on...

opengl es - Android GLSurfaceView glTexImage2D glDrawTexiOES

I'm trying to render a 640x480 RGB565 image using OpenGL ES on Android using GLSurfaceView and Native C code. Initially I had a 0x0501 error with glTexImage2D, which I was able to resolve by changing the image dimensions. But now, in the "drawFrame" call, when I do glDrawTexiOES to resnder the texture, I'm getting the following error on the Logs: drawtex.c:89: DrawTexture: No textures enabled

opengl es - How to overlay GLSurfaceView over a MapView in Android?

I want to create a simple Map based application in android ,where i can display my current position.Instead of overlaying a simple Image on the MapView to represent the position, i want to overlay the GLSurfaceView on the MapView. But i don't know how to achieve this. Is there any way to do that?. Please anybody knows the solution help me.

android - How to retain the state of a activity that has a GLSurfaceView

My problem is our game can switch into menu and setting mode instantly but it will need 4-6 seconds to load texture, init GL render mode eventually I just used 6 simple textures to create 6 sprites in game. Please help me answer two questions: 1. How can I preload our assets in android os to start our game quicker? 2. In order to use a trick to create instance switch between activity, how can I retain my activity w...

graphics - Differences and advantages of SurfaceView vs GLSurfaceView on Android?

I'm currently playing around with 2D graphics in android and have been using a plain old SurfaceView to draw Drawables and Bitmaps to the screen. This has been working alright, but there's a little stutter in the sprite movement, and I'm wondering the feasibility to do a real time (but not terrible fast) game with this. I know GLSurfaceView exists which uses OpenGL, but I'm curious as to the extent to which this m...

3d - Android OpenGL extending GLSurfaceView null pointer exceptions

I am trying to create a simple 3-D app for android that will have an additional view layered on top of the OpenGL view (much like the SurfaceViewOverlay example in the API demos). I'm running into an issue trying to implement that method with an extended GLSurfaceView class. I've set up an example where I'm trying to do a combination of

java - GLSurfaceView swaps 2D bitmaps being drawn

I have an application that receives information from a database, and is used to visualize 2D bitmaps onto a GLSurfaceView. The information received will determine the x-position of the bitmap, and which bitmap image to use (there are 4 different bitmaps in my res folder to choose from). Below are the three classes that are being used. The Activity sets the Shapes objects that need to be drawn by passing an ArrayLi...

opengl es - Android GLSurfaceView with drawable background

I have a GLSurfaceView with a drawable as background, however only the background is visible when rendered without surfaceView.setZOrderOnTop(true) I need to avoid using setZOrderOnTop(true) because there are static TextView's being used on top of the GLSurfaceView. Any suggestions for getting this to work?

android - GlSurfaceView is completly black when reloaded

In a nutshell, I use regular views for all my application except for on that uses a GLSurfaceView. the UI flow works well. I can navigate form one to the over except whith the GLSurfaceView when I open the first time the GLSurfaceView everything works fine, but when I switch to another view and come back (pause menu) my view is completly black... I tried several things and the closest I got...

android - Using a ViewAnimator with a glSurfaceView does not animate

I have a ViewAnimator setup to go through a few normal views (buttons, text, etc) and then to a glSurfaceView. When animating between the normal views it behaves as expected, however when trying to flip to the glSurfaceView it just appears. Perhaps it is not possible to animate a glSurfaceView. Here is how I setup the ViewAnimator &lt;ViewAnimator xmlns:android="

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android