I want to create a QR scan code system using ZXingScannerView, but i want to use this interface not in base class. I decided to create a xml file when I have just ZXingScannerView and I call functions on this, but it's not working.
This is my class when I run my Code Scanner by button click:
package com.example.elistazakupow3;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import Models.ShoppingList;
public class FragmentShoppingList extends Fragment {
protected View view;
private RecyclerView myRecyclerView;
private ArrayList<ShoppingList> lstShoppingList;
Button scan_button;
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_shopping_list, container,false);
myRecyclerView = view.findViewById(R.id.myRC);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MyRecyclerViewAdapter recyclerViewAdapter = new MyRecyclerViewAdapter(lstShoppingList);
myRecyclerView.setAdapter(recyclerViewAdapter);
//Scanner
scan_button = view.findViewById(R.id.scan_button);
scan_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CodeScanner();
}
});
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
lstShoppingList = new ArrayList<>();
lstShoppingList.add(new ShoppingList("Na wigilię","Nie wiem","23.04.1998"));
}
}
And my CodeScanner class
package com.example.elistazakupow3;
import android.app.AlertDialog;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class CodeScanner extends FragmentShoppingList implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
public CodeScanner(){
mScannerView = getActivity().findViewById(R.id.scaner);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void onPause(){
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
// Toast.makeText(getContext(), "Contents = " + result.getText() + ", Format = " + result.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
AlertDialog.Builder bd = new AlertDialog.Builder(getActivity());
bd.setTitle("Wynik skanowania");
bd.setMessage(result.getText());
AlertDialog pokazKod = bd.create();
pokazKod.show();
}
}
You can't show a Fragment in that manner. Read this and try to follow it in your code.
Move this to
onResume:ZXingScannerViewand return it in yourCodeScanner'sonCreateView, the idea is that instead of inflating an XML, you're instantiatingZXingScannerViewas your whole UI. Instead ofmScannerView = getActivity().findViewById(R.id.scaner);, do something like this in yourCodeScannerfragment: