0

So, I'm trying to make a login activity with the camera feed as the background. I've tested the example on https://developer.xamarin.com/guides/android/user_interface/textureview/ and got it to work, but I need to it to be part of an activity. So, I tried this:

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.Login);

        _textureView = this.FindViewById<TextureView>(Resource.Id.textureView);
        _textureView.SurfaceTextureListener = this;
    }

public void OnSurfaceTextureAvailable(
       Android.Graphics.SurfaceTexture surface, int w, int h)
        {
            _cam = Camera.Open();

            _textureView.LayoutParameters =
                   new FrameLayout.LayoutParams(w, h);

            try
            {
                _cam.SetPreviewTexture(surface);
                _cam.StartPreview();

            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

The axml file looks somewhat like this:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        AbsoluteLayout.LayoutBounds="0,0,1,1"
        AbsoluteLayout.LayoutFlags="SizeProportional" />
    <LinearLayout
        android:id="@+id/linearLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        AbsoluteLayout.LayoutBounds="0,0,1,1"
        AbsoluteLayout.LayoutFlags="SizeProportional">

        <!-- Some other controls -->
    </LinearLayout>
</AbsoluteLayout>

However, the app now crashes after exiting the OnSurfaceTextureAvailable block, giving an unhandled exception. Breaking at the exception doesn't work, it's apparently in a thread that's not running anymore. Would anyone know why it crashes and, more importantly, how to fix it?

Bart K
  • 115
  • 1
  • 2
  • 8

1 Answers1

0

Problem is with this code:

_textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);

I'm not sure why your need to reset the TextureView's layout parameters, in xml you've set it to match parent. And the parent here is AbsoluteLayout, not FrameLayout, that's why this error happens.

To solve this issue, since AbsoluteLayout is obsolete now, I suggest to use RelativeLayout as root container in your xml. Then you can code like this:

_textureView.LayoutParameters = new RelativeLayout.LayoutParams(width, height);

Of course you can also change AbsoluteLayout to FrameLayout in your xml, then the code _textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h); doesn't need to be modified anymore.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • Ah, simply taking that line out has fixed the error, and it's now showing a camera feed as I intended, many thanks. There's a different problem now, but I'll open another question for that since it's unrelated to this error. – Bart K May 01 '17 at 07:51