Ayad Kara Kâhya
Ayad Kara Kâhya

Reputation: 128

How to avoid turning my whole code into static

Am inheriting an interface called ISurfaceTextureListener in this code

class Camera
{
    TextureView mTextureView;
    Context _context;
    public Camera (Context context, TextureView textureView)
    {
        _context = context;
        mTextureView = textureView;
        mTextureView.SurfaceTextureListener = new TextureViewListener();
    }
    private class TextureViewListener : Java.Lang.Object, TextureView.ISurfaceTextureListener
    {
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            OpenCamera(width, height); //Error Occurring Here 
        }
    }
    public void OpenCamera(int width, int height)
    {
        //Codes 
    }
}

Error : an object reference is required to access non-static field, method etc..

I don't want to make OpenCamera() static because I will have to turn my whole code into static methods ,, so is there a way to avoid it ?

NOTE: am only inheriting the interface because I can't override object listener's "OnSurfaceTextureAvailable" method, the only way I found is to assign an inherited class for the object's listener and it worked just fine .

Upvotes: 0

Views: 77

Answers (1)

Mike Nakis
Mike Nakis

Reputation: 62045

The error is the exact opposite of what you think it is. It is not a hint that you should make OpenCamera() static; it is a hint that you are trying to access it as if it was static, while in fact it is not.

You need someObjectIhaventToldYouAnythingAbout.OpenCamera(width, height);

EDIT

So, after your comment and your edits, where you are essentially telling us about someObjectIhaventToldYouAnythingAbout, it appears that you should be doing this:

    mTextureView.SurfaceTextureListener = new TextureViewListener( this );
}
private class TextureViewListener : Java.Lang.Object, TextureView.ISurfaceTextureListener
{
     readonly Camera camera;

     TextureViewListener( Camera camera )
     {
         this.camera = camera;
     }

    public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
    {
        camera.OpenCamera(width, height); //Error Occurring Here 
    }
}
public void OpenCamera(int width, int height)
{
    //Codes 
}

Upvotes: 3

Related Questions