Android 条码扫描仪 - onActivityResult

Android Barcode scanner - onActivityResult

m using the ZXing barcode scanner and I have some issues. Ithis路口之后,我不明白我应该在哪里调用"onActivityResult"方法,我需要发送哪个参数。

我尝试将调用放在 OnClickListener 下,但我总是从我的 intentResult 中得到 null。

这是我的代码:

    public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment implements View.OnClickListener {

        Button scan;
        TextView t1, t2;
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            scan = (Button)rootView.findViewById(R.id.bScan);
            t1 = (TextView) rootView.findViewById(R.id.tv1);
            t2 = (TextView) rootView.findViewById(R.id.tv2);

            t1.setText("OK");
            scan.setOnClickListener(this);
            return rootView;
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
            if (intentResult != null ){

                String scanContact = intentResult.getContents();
                String scanFormat = intentResult.getFormatName();


                String contact = "Contact is: " + scanContact +"\n";
                String foramt = "Format is: " + scanFormat;

                --- I never get to this part --- 

                t1.setText(contact);
                t2.setText(foramt);
            } else {
                Toast toast = Toast.makeText(getActivity(),
                        "No scan data received!", Toast.LENGTH_SHORT);
                toast.show();
                t1.setText("Not OK");
            }
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.bScan) {
                IntentIntegrator intentIntegrator = new IntentIntegrator(getActivity());
                intentIntegrator.initiateScan();

             //   Intent intent = getActivity().getIntent();
                //startActivityForResult(intent,getTargetRequestCode());
                //onActivityResult(getTargetRequestCode(),CONTEXT_INCLUDE_CODE,intent);

            }
        }
    }
}

有什么帮助吗? 谢谢

托管您的 Fragment 的 MainActivity 获取结果。你应该在MainActivity中调用onActivityResult,并调用super,这样你的Fragment才有机会处理数据。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}