gpt4 book ai didi

java - 在 fragment 中获取结果的问题

转载 作者:行者123 更新时间:2023-12-02 01:33:42 24 4
gpt4 key购买 nike

美好的一天,我试图获取扫描 fragment 中二维码的结果,作为 Activity 一切正常,但传输后无法获取结果。如何解决这个问题?

据我了解“onActivityResult”方法中的问题。我需要一些信息来解决问题。如何更改 fragment 的它?

我的代码位于以下行:

public class Fragment_qr extends Fragment implements View.OnClickListener {

private Button btnScanQRCode;
private TextView txtFruit, txtSize, txtColor;

//QR Code Scanner Object
private IntentIntegrator qrScan;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {



return inflater.inflate(R.layout.qr_code_scan_layout, null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

super.onViewCreated(view, savedInstanceState);
DisplayMetrics metrics = this.getResources().getDisplayMetrics();

btnScanQRCode = (Button) view.findViewById(R.id.btnScanQRCode);
txtFruit = (TextView) view.findViewById(R.id.txtFruitName);
txtSize = (TextView) view.findViewById(R.id.txtFruitSize);
txtColor = (TextView) view.findViewById(R.id.txtFruitColor);

//Initialize the Scan Object
qrScan = new IntentIntegrator(getActivity());

//OnClickListener Attached to the Button
btnScanQRCode.setOnClickListener(this);

}

//Getting the scan results
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//Check to see if QR Code has nothing in it
if (result.getContents() == null) {
Toast.makeText(getActivity(), "Result Not Found", Toast.LENGTH_LONG).show();
} else {
//QR Code contains some data
try {
//Convert the QR Code Data to JSON
JSONObject obj = new JSONObject(result.getContents());
//Set up the TextView Values using the data from JSON
txtFruit.setText(obj.getString("fruit"));
txtSize.setText(obj.getString("size"));
txtColor.setText(obj.getString("color"));
} catch (JSONException e) {
e.printStackTrace();
//In case of exception, display whatever data is available on the QR Code
//This can be caused due to the format MisMatch of the JSON
Toast.makeText(getActivity(), result.getContents(), Toast.LENGTH_LONG).show();
txtSize.setText(result.getContents());
System.out.println("code QR: " + result.getContents());
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);

}
}

@Override
public void onClick(View view) {

//initiating the qr code scan
qrScan.initiateScan();

}
}

最佳答案

此替换有帮助:

    public void onClick(View view) {
IntentIntegrator.forFragment(this).initiateScan();
}

关于java - 在 fragment 中获取结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55574715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com