После обычного сканирования QR-кода он перестает работать и не может отображать отсканированное содержимое.
Сканер штрих-кода (Main2Activity. java):
package com.example.barcodescanner;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class Main2Activity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
@Override
public void handleResult(Result rawResult) {
// Do something with the result here
MyFragment.textViewQr.setText(rawResult.getText());
onBackPressed();
}
}
Вот мой фрагмент, который использует выше (MyFragment. java) :
package com.example.barcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
/**
* A simple {@link Fragment} subclass.
*/
public class MyFragment extends Fragment {
public MyFragment() {
// Required empty public constructor
}
static TextView textViewQr;
Button buttonScanner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_my,container,false);
final TextView textViewQr = (TextView) view.findViewById( R.id.textViewQr );
final Button buttonScanner = (Button) view.findViewById( R.id.buttonScanner );
buttonScanner.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity( new Intent( getActivity(), Main2Activity.class ) );
}
} );
return view;
}
}
Logcat:
2020-03-13 10:54:50.267 20894-20927/com.example.Barcodescanner D/EGL_emulation: eglMakeCurrent: 0xa08052a0: ver 2 0 (tinfo 0xa0803340)
2020-03-13 10:54:52.959 20894-20894/com.example.Barcodescanner D/AndroidRuntime: Shutting down VM
--------- beginning of crash
2020-03-13 10:54:52.959 20894-20894/com.example.Barcodescanner E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.Barcodescanner, PID: 20894
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.Barcodescannerwith.Main2Activity.handleResult(Main2Activity.java:37)
at me.dm7.barcodescanner.zxing.ZXingScannerView$1.run(ZXingScannerView.java:164)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
После обычного сканирования QR-кода он перестает работать, и следующие советы:
Barcodescanner снова остановил открытое приложение