Abhishek Bansal
Abhishek Bansal

Reputation: 5335

setContentView() Crashes when used with GLSurfaceView

I have been trying to make my first android app. I am stuck with this strange problem. There are lot of people who had faced same problem (like here, here, here and lots of other places) in past but none of the solutions seems to work for me.

My problem is if I set layout file like setContentView(R.layout.MainActivity) application crashes on this function but if I directly set GLSurfaceView as content view application works just fine. I want to have a ListView and GLSurfaceView on same screen thats why I am trying to add it in XML.

Here is my layout file

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.example.camerafilters.CameraGLSurfaceView 
  android:id="@+id/CameraGLSurfaceView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

<!-- <ListView
android:id="@+id/ShaderList"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> 
  -->                                 
 </LinearLayout>

Here is relevant portion from main activity. Note that CameraGLSurfaceView is an inner class.

public class CameraMainActivity extends Activity implements
SurfaceTexture.OnFrameAvailableListener 
{
private CameraGLSurfaceView _cameraGLView;

/**
 * Captures frames from an image stream as an OpenGL ES texture. The image stream may come from either camera preview or video decode.
 *  A SurfaceTexture may be used in place of a SurfaceHolder when specifying the output destination of a Camera or MediaPlayer object
 */
private SurfaceTexture _surface;

CameraGLRenderer _renderer;

private Camera _camera;

private ListView _shaderListView;

class CameraGLSurfaceView extends GLSurfaceView 
{
    CameraGLRenderer renderer;
    public CameraGLSurfaceView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        //  Create an OpenGL ES 2.0 context
        setEGLContextClientVersion(2);

        // Set the Renderer for drawing on the GLSurfaceView
        renderer = new CameraGLRenderer((CameraMainActivity)context);
        setRenderer(renderer);

        //  Render the view only when there is a change in the drawing data
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    public CameraGLRenderer getRenderer()
    {
        return renderer;
    }
}


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    _cameraGLView = new CameraGLSurfaceView(this, null);
    // Create a GLSurfaceView instance and set it
    // as the ContentView for this Activity. 
    setContentView(R.layout.activity_main);

    _cameraGLView = (CameraGLSurfaceView) findViewById(R.id.CameraGLSurfaceView);
    _renderer = _cameraGLView.getRenderer();

}

Any pointers where i am wrong ?

Upvotes: 0

Views: 860

Answers (3)

user6996362
user6996362

Reputation:

You should call GLSurfaceView.setRenderer() in your Activity.onCreate()

Upvotes: 0

laalto
laalto

Reputation: 152817

First, have a look at the exception stacktrace in logcat to find the exact problem.

Then, guessing the problem is that the view specified in your XML cannot be instantiated: your inner class needs to be public static and you need to refer to it correctly in XML, like

com.example.camerafilters.CameraMainActivity$CameraGLSurfaceView

though it's cleaner to have it as a separate class instead of an inner class.

Upvotes: 1

Kosh
Kosh

Reputation: 6334

Why u calling it before onCreate ? You don't need to call it since you defined it in your xml layout. You dont need to call it all. Just delete the line before setContentView.

Upvotes: 0

Related Questions