我想使用ZXingScannerView创建QR扫描代码系统,但是我不想在基类中使用此接口。当我只有ZXingScannerView并在其上调用函数时,我决定创建一个xml文件,但是它不起作用。

通过单击按钮运行代码扫描程序时,这是我的课程:

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"));

    }

}


还有我的CodeScanner类

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();
    }
}

最佳答案

您不能以这种方式显示片段。阅读this并尝试在您的代码中遵循它。
将此移至onResume


        mScannerView.setResultHandler(this);
        mScannerView.startCamera();



您应该创建一个新的ZXingScannerView并将其返回到CodeScanneronCreateView中,其想法是要实例化ZXingScannerView作为整个UI,而不是膨胀XML。代替mScannerView = getActivity().findViewById(R.id.scaner);,在您的CodeScanner片段中执行以下操作:


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mScannerView= new ZXingScannerView(requireContext());
        return mScannerView;
    }

10-07 16:57