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.
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:If you are implementing a fragment-class, note that you have to call
getActivity()
when creating theIntentIntegrator
.You are referencing your
OnClickListener
and not yourFragment
when you saythis
inside youronClick
. You need to reference yourFragment
, the easiest way to do that is setup a global variable and call itmyFragment
or something to your liking, then assign itthis
. For example:following can work: