ZXING Android Embedded Usage of IntentIntegrator

江枫思渺然 提交于 2019-12-04 21:10:46

An even easier solution than ChrisStillwell´s would be to make your activity-/fragment-class implement the OnClickListener, this way you do not need the reference variable:

public class SomeFragment extends Fragment implements View.OnClickListener {
    // Rest of your code

    @Override
    public void onClick(View v) {

         if (v.getId == button.getId) {
              IntentIntegrator integrator = new IntentIntegrator(getActivity());
              IntentIntegrator.forFragment(this).initiateScan();
         }
    }
}

If you are implementing a fragment-class, note that you have to call getActivity() when creating the IntentIntegrator.

You are referencing your OnClickListener and not your Fragment when you say this inside your onClick. You need to reference your Fragment, the easiest way to do that is setup a global variable and call it myFragment or something to your liking, then assign it this. For example:

Fragment myFragment = this;

public void myFunction(){
    // Code and stuff //
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            IntentIntegrator integrator = new IntentIntegrator(this);
            IntentIntegrator.forFragment(myFragment).initiateScan();
        }
    });
}

following can work:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new IntentIntegrator(getActivity()).initiateScan();
                popupWindow.dismiss();

            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!