我是Android新手。我的扫描应用程序无法在片段中调用onActivityResult。我确实检查了其他线程,但无法理解这个问题。
public class BarCodeFrag extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment SearchCatFragment.
*/
// TODO: Rename and change types and number of parameters
public static BarCodeFrag newInstance(String param1, String param2) {
BarCodeFrag fragment = new BarCodeFrag();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public BarCodeFrag() {
// Required empty public constructor
}
// @Override
// public void startActivityForResult(Intent intent, int code) {
// BarCodeFrag.startActivityForResult(intent, code);
// }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
//the custom view
private SearchView myView;
private Button scanBtn;
private TextView formatTxt, contentTxt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_barcode, container, false);
//get reference to the custom view
// myView = (SearchView) rootView.findViewById(R.id.androidATCView1);
scanBtn = (Button) rootView.findViewById(R.id.scan_button);
formatTxt = (TextView) rootView.findViewById(R.id.scan_format);
contentTxt = (TextView) rootView.findViewById(R.id.scan_content);
scanBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId()==R.id.scan_button){
//instantiate ZXing integration class
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
//start scanning
scanIntegrator.initiateScan();
// startActivityForResult(scanIntegrator, 1888);
}
else{
//invalid scan data or scan canceled
Toast toast = Toast.makeText(getActivity().getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
});
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve result of scanning - instantiate ZXing object
super.onActivityResult(requestCode, resultCode, intent);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
//check we have a valid result
Toast.makeText(getActivity().getApplicationContext(), "Inside onActivityResult" , Toast.LENGTH_LONG).show();
if (scanningResult != null) {
//get content from Intent Result
String scanContent = scanningResult.getContents();
//get format name of data scanned
String scanFormat = scanningResult.getFormatName();
//output to UI
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
Toast.makeText(getActivity().getApplicationContext(), "Reply: " + scanFormat + ":" + scanContent, Toast.LENGTH_LONG).show();
CheckAvailability(scanFormat);
}}
我打算从扫描中看到格式和内容。这个appl正在运行一个单独的appl,但是当我在片段中添加代码以添加到我的其他应用程序时 - 我无法看到格式和内容。欢迎任何建议。非常感谢。