Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

myoverview.xml


<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <android.opengl.GLSurfaceView 
            android:id="@+id/cubes" 
            android:orientation="horizontal" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/> 
    <SurfaceView 
            android:id="@+id/camera" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/> 
</FrameLayout>

myoverview.java


import android.app.Activity; 
import android.os.Bundle; 
import android.view.SurfaceView; 
import android.view.Window; 
public class MyOverView extends Activity { 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       // Hide the window title. 
       requestWindowFeature(Window.FEATURE_NO_TITLE); 
       // camera view as the background 
       SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera); 
       cameraView = new CameraView(this); 
       // visual of both cubes 
       GLSurfaceView cubesView = (GLSurfaceView) findViewById(R.id.cubes); 
       cubesView = new GLSurfaceView(this); 
       cubesView.setRenderer(new CubeRenderer(false)); 
       // set view 
       setContentView(R.layout.myoverview); 
    } 
}

GLSurfaceView.java


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

NOTE :

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

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

like image 585
pohtzeyun Avatar asked Jan 21 '10 10:01

pohtzeyun


2 Answers

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..

like image 127
bala singareddy Avatar answered Sep 30 '22 09:09

bala singareddy


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.

like image 41
Jlam Avatar answered Sep 30 '22 09:09

Jlam