title: android.view.InflateException: Binary XML file line #34: Error inflating class AutoFitTextureView
body: <p>I get a crash on some devices because of Inflate exception, I'm trying to inflate a custom class called AutoFitTextureView, I get this Logcat message:</p>
  
  <pre><code>android.view.InflateException: Binary XML file line #34: Binary XML file line #34: Error inflating class com.xscoder.pikky.AutoFitTextureView
  </code></pre>
  
  <p>I'm using the Camera2 Basic code from google to open a custom camera.</p>
  
  <p>Here's part of my <strong>square_camera.xml</strong> layout:</p>
  
  <pre><code>&lt;com.xscoder.pikky.AutoFitTextureView
      android:id="@+id/texture"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/sqcTopView"
      android:layout_centerHorizontal="true"
      android:background="#333"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"&gt;
  &lt;/com.xscoder.pikky.AutoFitTextureView&gt;
  </code></pre>
  
  <p>Here's the code of my <strong>AutoFixTextureView</strong> class:</p>
  
  <pre><code>public class AutoFitTextureView extends TextureView {
  
  
      private int mRatioWidth = 0;
      private int mRatioHeight = 0;
  
      public AutoFitTextureView(Context context) {
          this(context, null);
      }
  
      public AutoFitTextureView(Context context, AttributeSet attrs) {
          this(context, attrs, 0);
      }
  
      public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
  
  
  
      public void setAspectRatio(int width, int height) {
          if (width &lt; 0 || height &lt; 0) {
              throw new IllegalArgumentException("Size cannot be negative.");
          }
          mRatioWidth = width;
          mRatioHeight = height;
          requestLayout();
      }
  
  
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  
          int width = MeasureSpec.getSize(widthMeasureSpec);
          int height = MeasureSpec.getSize(heightMeasureSpec);
          if (0 == mRatioWidth || 0 == mRatioHeight) {
              setMeasuredDimension(width, height);
          } else {
              if (width &lt; height * mRatioWidth / mRatioHeight) {
                  setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
              } else {
                  setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
              }
          }
      }
  
  
  }// ./ end
  </code></pre>
  
  <p>And here's where I call it:</p>
  
  <pre><code>public class SquareCamera extends AppCompatActivity {
  
  private AutoFitTextureView mTextureView;
  ...
  protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.square_camera);
  
          mTextureView = findViewById(R.id.texture);
  </code></pre>
  
  <p>I really can't figure out what's wrong, because if I run my app on a real Huawei Cam 21 with Android 6.0 or a Quilive with Android 5.1, the app doesn't crash at all, but on other devices, and also an Android 8.0 emulator, it crashes with that error.</p>