我试图将Zbar扫描仪的Camera Preview放入TextureView,但Android Studio仍然会返回渲染错误。
"渲染期间引发异常:android.view.TextureView无法强制转换为android.view.ViewGroup"
如何修复它。
<TextureView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/cameraView"
android:layout_below="@+id/text_read_barcode">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
app:zxing_use_texture_view = "true"
app:zxing_preview_scaling_strategy="centerCrop">
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
</TextureView>
此处的活动代码
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.TextureView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.ResultPoint;
import com.google.zxing.client.android.BeepManager;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import java.util.ArrayList;
import java.util.List;
public class MoveIssueReadBarcode extends AppCompatActivity {
private Button mButtonHome, mButtonBack, mButtonRead;
ListView listView;
CustomListAdapter adapter;
List<Items> row;
TextView setUser, setBase;
TextureView cameraView;
TextView barcodeInfo;
DecoratedBarcodeView barcodeView;
private BeepManager beepManager;
private String lastText;
private BarcodeCallback callback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
if(result.getText() == null || result.getText().equals(lastText)) {
// Prevent duplicate scans
return;
}
lastText = result.getText();
barcodeView.setStatusText(result.getText());
beepManager.playBeepSoundAndVibrate();
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move_issue_read_barcode);
Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar3 );
setSupportActionBar( toolbar );
//Barcode reading
cameraView = (TextureView) findViewById(R.id.cameraView);
barcodeInfo = (TextView) findViewById(R.id.codeInfo);
barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
beepManager = new BeepManager(this);
}
@Override
protected void onResume() {
super.onResume();
barcodeView.resume();
}
@Override
protected void onPause() {
super.onPause();
barcodeView.pause();
}
public void pause(View view) {
barcodeView.pause();
}
public void resume(View view) {
barcodeView.resume();
}
public void triggerScan(View view) {
barcodeView.decodeSingle(callback);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
private void doInit(){
}
}