android.view.InflateException:二进制XML文件行#34:错误膨胀类AutoFitTextureView

问题描述 投票:1回答:1

由于Inflate异常,我在某些设备上崩溃了,我试图给自定义类增加一个名为AutoFitTextureView的东西,我得到这个Logcat消息:

android.view.InflateException: Binary XML file line #34: Binary XML file line #34: Error inflating class com.xscoder.pikky.AutoFitTextureView

我正在使用谷歌的Camera2 Basic代码打开自定义相机。

这是我的square_camera.xml布局的一部分:

<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">
</com.xscoder.pikky.AutoFitTextureView>

这是我的AutoFixTextureView类的代码:

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 < 0 || height < 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 < height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            }
        }
    }


}// ./ end

这就是我所说的:

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);

我真的无法弄清楚出了什么问题,因为如果我在真正的华为Cam 21上使用Android 6.0或者使用Android 5.1的Quilive运行我的应用程序,该应用程序根本不会崩溃,但在其他设备上,以及Android 8.0模拟器,它与该错误崩溃。

java android camera binary layout-inflater
1个回答
1
投票

通过简单地编辑我的square_camera.xml文件来修复该错误,如下所示:

<com.xscoder.pikky.AutoFitTextureView
    android:id="@+id/texture"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/sqcTopView"
   >
</com.xscoder.pikky.AutoFitTextureView>

似乎有些Android设备没有处理自定义TextureView的附加属性,所以最好用Java编程设置:)

© www.soinside.com 2019 - 2024. All rights reserved.