Java 在片段中获取结果的问题
您好,我正在尝试获得扫描片段中二维码的结果,因为活动一切正常,但传输后无法获得结果。如何解决这个问题 正如我在“onActivityResult”方法中理解的那样。我需要一些信息来解决问题。如何为片段更改它 我的代码在下面一行:Java 在片段中获取结果的问题,java,android,android-fragments,fragment,qr-code,Java,Android,Android Fragments,Fragment,Qr Code,您好,我正在尝试获得扫描片段中二维码的结果,因为活动一切正常,但传输后无法获得结果。如何解决这个问题 正如我在“onActivityResult”方法中理解的那样。我需要一些信息来解决问题。如何为片段更改它 我的代码在下面一行: public class Fragment_qr extends Fragment implements View.OnClickListener { private Button btnScanQRCode; private TextView txtFruit, t
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();
}
这种替代有助于:
public void onClick(View view) {
IntentIntegrator.forFragment(this).initiateScan();
}
super.onActivityResult
应放在onActivityResult
function@JohnJoe,为什么onActivityResult值得这样发布?您在哪里调用startActivityForResult()
?如果要在片段中使用活动结果,则需要在该特定片段上调用它。@kg。在哪里添加?如果出现“onClick”?是,只需在那里调用startActivityForResult
并为其提供您提供的参数,则应传递结果。super.onActivityResult
应放在onActivityResult
之后function@JohnJoe,为什么onActivityResult值得这样发布?您在哪里调用startActivityForResult()
?如果要在片段中使用活动结果,则需要在该特定片段上调用它。@kg。在哪里添加?如果出现“onClick”?是,只需在那里调用startActivityForResult
并为其提供您为其提供的参数,则应提供结果。