嗨我有一个扫描QR码的应用程序,因此它需要相机权限。我有一个SurfaceView,我在其中显示相机预览。
在清单中我添加了:<uses-permission android:name="android.permission.CAMERA"></uses-permission>
在我的扫描片段中,我有这段代码:
public class Tab2Scan extends Fragment {
private static final int PERMS_REQUET_CODE = 123;
// QREader
private SurfaceView mySurfaceView;
private QREader qrEader;
Context thisContext;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tab2scan, container, false);
if(hasPermissions())
{
startReading();
}
else
{
requestPerms();
}
return rootView;
}
private boolean hasPermissions()
{
int res = 0;
String [] permissions = new String[]{Manifest.permission.CAMERA};
for (String perms : permissions)
{
res = checkCallingOrSelfPermission(getContext(),perms);
if(!(res == PackageManager.PERMISSION_GRANTED))
{
return false;
}
}
return true;
}
private void requestPerms()
{
String [] permissions = new String[]{Manifest.permission.CAMERA};
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(permissions, PERMS_REQUET_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
boolean allowed = true;
switch (requestCode)
{
case PERMS_REQUET_CODE:
for(int res:grantResults)
{
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
allowed = false;
break;
}
if(allowed)
{
startReading();
}
else
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
{
Toast.makeText(thisContext, "Abilita l'accesso alla fotocamera", Toast.LENGTH_SHORT).show();
}
}
}
}
private void startReading()
{
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
thisContext = getContext();
mySurfaceView = (SurfaceView) rootView.findViewById(R.id.camera_view);
// Init QREader
// ------------
qrEader = new QREader.Builder(thisContext, mySurfaceView, new QRDataListener() {
@Override
public void onDetected(final String data) {
//qrEader.stop();
Intent intent = new Intent(getActivity(), ShowData.class);
intent.putExtra("variabile", data);
startActivity(intent);
getActivity().finish();
//Toast.makeText(thisContext, result.getContents(),Toast.LENGTH_LONG).show();
Log.d("QREader", "Value : " + data);
}
}).facing(QREader.BACK_CAM)
.enableAutofocus(true)
.height(mySurfaceView.getHeight())
.width(mySurfaceView.getWidth())
.build();
qrEader.start();
}
@Override
public void onResume() {
super.onResume();
// Init and Start with SurfaceView
// -------------------------------
qrEader.initAndStart(mySurfaceView);
qrEader.start();
}
@Override
public void onPause() {
super.onPause();
// Cleanup in onPause()
// --------------------
qrEader.releaseAndCleanup();
}
}
当我发布片段时,它崩溃说:java.lang.NullPointerException: Attempt to invoke virtual method 'void github.nisrulz.qreader.QREader.initAndStart(android.view.SurfaceView)' on a null object reference
任何人都知道如何解决?谢谢!