如何在要求摄像机许可时阻止zxing QR扫描仪崩溃?

时间:2017-09-24 20:52:07

标签: java android permissions crash

所以我正在尝试创建一个应用程序,用于与服务器通信,通过网络发送QR扫描结果。此刻我可以登录。但是当我尝试请求运行时权限来使用相机时,我的错误日志表明它由于nullpointer异常而无法调用onPause()方法。虽然我已经指定在未授予权限时不启动QR扫描程序...所以我猜这是UI线程崩溃。单击扫描按钮时,将提示权限窗口(方法getScanPermit()),如果是,则调用ScanQR()方法,初始化扫描程序。这是我的代码:

public class Client extends AppCompatActivity implements ZXingScannerView.ResultHandler{
public static final int CONNECTIONPORT = 5000;
public static final int CAMERA_REQUEST_CODE = 10;
private Socket socket = null;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
private ZXingScannerView scannerView;
public boolean connected = false;

private static String ip = "192.168.0.191";
String message = "";
EditText username;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.loginscene);
    username = (EditText) findViewById(R.id.usernameText);

    final Button loginbutton = (Button) findViewById(R.id.loginbutton);
    loginbutton.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View v) {
            InitConnection();
        }
    });

}



public void InitConnection() {

    try {
        socket = new Socket(ip, CONNECTIONPORT);
        outputStream = new ObjectOutputStream(socket.getOutputStream());
        outputStream.flush();
        inputStream = new ObjectInputStream(socket.getInputStream());
        connected = true;
        socket.setSoTimeout(1000);
    } catch (IOException connecterror) {
        Toast.makeText(getApplicationContext(), "could not connect to server", Toast.LENGTH_SHORT).show();
    }

    send("login");
    send(username.getText().toString());

    try {
        String stringy = (String) inputStream.readObject();
        if (stringy.equals("granted")) {
            setContentView(R.layout.connectedscene);
        } else {
            Toast.makeText(getApplicationContext(), "Invalid Username", Toast.LENGTH_SHORT).show();
            outputStream.close();
            inputStream.close();
            socket.close();
        }
    } catch (ClassNotFoundException | IOException e1) {
        Toast.makeText(getApplicationContext(), "could not read answer from server", Toast.LENGTH_SHORT).show();
    }


}

public void send(Object o) {
    if (connected) {
        try {
            outputStream.reset();
            outputStream.writeObject(o);
            outputStream.flush();
        } catch (IOException e) {
            connected = false;
            Toast.makeText(getApplicationContext(), "could not send data to server", Toast.LENGTH_SHORT).show();
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e3) {
                Toast.makeText(getApplicationContext(), "could not close streams", Toast.LENGTH_SHORT).show();
            }
        }
    } else {
        Toast.makeText(getApplicationContext(), "you are not connected to the server", Toast.LENGTH_SHORT).show();
    }
}

public void getScanPermit(View v){
    if(checkSelfPermission(Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED){
        ScanQR();
    }else{
        String[] permissionRequested = {Manifest.permission.CAMERA};
        requestPermissions(permissionRequested, CAMERA_REQUEST_CODE);
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode==CAMERA_REQUEST_CODE){
        if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
            ScanQR();
        }else{
            Toast.makeText(getApplicationContext(),"Unable to scan QR without camera permission",Toast.LENGTH_SHORT).show();
        }
    }
}

public void ScanQR(){
    scannerView = new ZXingScannerView(this);
    setContentView(scannerView);
    scannerView.setResultHandler(this);
    scannerView.startCamera();
}

@Override
protected void onPause() {
    super.onPause();
    scannerView.stopCamera();
}

@Override
public void handleResult(Result result) {
    Toast.makeText(getApplicationContext(), result.getText().toString(), Toast.LENGTH_SHORT).show();
}}

这是错误日志(对不起白空间):

E/AndroidRuntime: FATAL EXCEPTION: main Process: 
                                                                    java.lang.RuntimeException: Unable to pause activity {hofman.jasper.app/hofman.jasper.app.Client}: java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null object reference
                                                                        at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:3751)
                                                                        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3717)
                                                                        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3691)
                                                                        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3665)
                                                                        at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1489)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null object reference
                                                                        at hofman.jasper.app.Client.onPause(Client.java:158)
                                                                        at android.app.Activity.performPause(Activity.java:6819)
                                                                        at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1322)
                                                                        at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:3740)
                                                                        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3717) 
                                                                        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3691) 
                                                                        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3665) 
                                                                        at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1489) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我希望你能提前给我一些提示!谢谢!

0 个答案:

没有答案