使用片段的Zxing库中的对话框

时间:2018-10-21 18:45:04

标签: java android

我正在片段中使用Zxing扫描仪,并在对话框中显示扫描仪结果,然后在Google上搜索该结果并单击取消按钮。但是取消按钮dnt可以正常工作。它给出错误错误是 尝试在空对象引用上调用虚拟方法'android.content.res.Resources $ Theme android.content.Context.getTheme()' 完整的代码在这里 我在下面的这一行上收到错误 AlertDialog.Builder builder =新的AlertDialog.Builder(getActivity());

package com.ali.mano.viatick;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.google.zxing.Result;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

import static android.Manifest.permission.CAMERA;


public class BarcodeFragment extends Fragment implements ZXingScannerView.ResultHandler {

    View view;
LinearLayout lin_cameraview;
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView scannerView;

private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;




@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.layout_barcodefragment, container, false);
    lin_cameraview = (LinearLayout) view.findViewById(R.id.lnear_camera);
    scannerView = new ZXingScannerView(getActivity());
scannerView.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT));
lin_cameraview.addView(scannerView);

int currentApiVersion = Build.VERSION.SDK_INT;

if (currentApiVersion >= Build.VERSION_CODES.M) {
    if (checkPermission()) {
        Toast.makeText(getContext(), "Permission already granted!", Toast.LENGTH_LONG).show();
    } else {
        requestPermission();
    }
}
return view;

}

private boolean checkPermission() {
    return (ContextCompat.checkSelfPermission(getContext(), CAMERA) == PackageManager.PERMISSION_GRANTED);
}

private void requestPermission() {
    ActivityCompat.requestPermissions(getActivity(), new String[]{CAMERA}, REQUEST_CAMERA);
}

@Override
public void onResume() {
    super.onResume();

    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
        if (checkPermission()) {
            if (scannerView == null) {
                scannerView = new ZXingScannerView(getActivity());
                lin_cameraview.addView(scannerView);
            }
            scannerView.setResultHandler(this);
            scannerView.startCamera();
        } else {
            requestPermission();
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    scannerView.stopCamera();
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CAMERA:
            if (grantResults.length > 0) {

                boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                if (cameraAccepted) {
                    Toast.makeText(getContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (shouldShowRequestPermissionRationale(CAMERA)) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                            builder.setMessage("You need to allow access to both the permissions");
                            builder.setPositiveButton("Ok!!", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                        requestPermissions(new String[]{CAMERA},
                                                REQUEST_CAMERA);
                                    }
                                }
                            });
                            builder.show();

@Override
public void handleResult(Result result) {
    final String myResult = result.getText();
    Log.d("QRCodeScanner", result.getText());
    Log.d("QRCodeScanner", result.getBarcodeFormat().toString());

    **

> AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

**
    builder.setTitle("Scan Result");
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            scannerView.resumeCameraPreview((ZXingScannerView.ResultHandler) new BarcodeFragment());

        }
    });
    builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            String escapedQuery = null;
            try {
                escapedQuery = URLEncoder.encode(myResult, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            Uri uri = Uri.parse("http://www.google.com/#q=" + escapedQuery);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);



 }
});
builder.setMessage(result.getText());
builder.show();
}

0 个答案:

没有答案