ZXING Android Embedded Usage of IntentIntegrator

2019-04-17 10:05发布

问题:

I am new to Android development and still trying to get the grasp of some of the concepts. I find the best way to learn is to jump right into the deep end with a project. With that said, here is my question:

I have integrated ZXing Android Embedded into my application; however, I am having trouble understanding the way in which you use IntentIntegrator. All I am trying to do at the moment is call the QR scanner to the screen when the user taps a button. I have been trying to follow the instructions on their github link [here][1] but have been unsuccessful.

Here is what my function looks like so far:

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            IntentIntegrator integrator = new IntentIntegrator(this);
            IntentIntegrator.forFragment(this).initiateScan();

        }

    });

I keep getting an error saying:

Error:(109, 25) error: constructor IntentIntegrator in class IntentIntegrator cannot be applied to given types; required: Activity found: Intent reason: actual argument Intent cannot be converted to Activity by method invocation conversion

Also, as I put my mouse over '(this)' in Android Studio, it says:

anonymous android.view.View.onClickListener

Any help would be greatly appreciated, thanks! If you need any other information, please let me know.

回答1:

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();
        }
    });
}


回答2:

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.



回答3:

following can work:

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

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

            }
        });