Build error: ZapparEmbed intent

Building an app in React-Native and try to include the Zappar module on Android first.
I get the following error.

no suitable constructor found for Intent(Class<CAP#1>)
            Intent i = new Intent(ZapparEmbed.getZapcodeClassForIntent());
                       ^
    constructor Intent.Intent(Intent) is not applicable
      (argument mismatch; Class<CAP#1> cannot be converted to Intent)
    constructor Intent.Intent(String) is not applicable
      (argument mismatch; Class<CAP#1> cannot be converted to String)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

Hi!

It looks like you’re missing the first parameter for the Intent, which should be your Context (e.g. your Activity):

Intent i = new Intent(this, ZapparEmbed.getZapcodeClassForIntent());

Let us know if that solves things for you :smile: .

This is my current code so far.

public class ZapparModule extends ReactContextBaseJavaModule {
    private ReactApplicationContext _context;

    public ZapparModule (ReactApplicationContext reactContext) {
        super(reactContext);
        this._context = reactContext;
    }

    @Override
    public String getName() {
        return "Zappar";
    }

    @ReactMethod
    public void InitZappar() {

        boolean isCompatible = ZapparEmbed.isCompatible(getReactApplicationContext());
        if (isCompatible) {

            Intent i = new Intent(this._context, ZapparEmbed.getZapcodeClassForIntent());
            getReactApplicationContext().startActivity(i);
        }
    }
}

im getting this error when i click on my button build in React.

calling startactivity() from outside of an activity context requires the flag_activity_new_task flag. Is this really what you want?

Hi,

It looks like you’re using an Application context in your Intent. When starting a new Activity (like the Zappar one in this case) Android likes to use an existing Activity context so it can show the Activities as part of one task to the user (so that it appears as one application in the recent applications list etc).

We don’t have any React Native experience here but we’ve taken a quick look at the source for the base class (available here) and it looks like there’s a function you can use to get an Activity, getCurrentActivity(). We’d recommend trying the following:

    @ReactMethod
    public void InitZappar() {
        Activity currentActivity = getCurrentActivity();
        if (currentActivity == null) {
            // Perhaps start the activity using an Intent and setting the new task flag
            return;
        }
        boolean isCompatible = ZapparEmbed.isCompatible(getReactApplicationContext());
        if (isCompatible) {
            Intent i = new Intent(currentActivity, ZapparEmbed.getZapcodeClassForIntent());
            getReactApplicationContext().startActivity(i);
        }
    }

Cheers,
Connell

i’m still getting the same error :frowning:

http://stackoverflow.com/a/3918838/6721125

I’ve tried too FLAG_ACTIVITY_NEW_TASK

Intent i = new Intent(currentActivity, ZapparEmbed.getZapcodeClassForIntent());
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getReactApplicationContext().startActivity(i);

but no results. I have too little knowledge to try the other two options.

Hi,

The flag isn’t necessary if you use an Activity context so it should be OK to remove that. I also just realised you should use call currentActivity.startActivity(i) rather than using your application context, e.g.:

Intent i = new Intent(currentActivity, ZapparEmbed.getZapcodeClassForIntent());
currentActivity.startActivity(i);

Cheers,
Connell

then my application will crash when i hit the button to initzappar()

Have you added the correct API key to your AndroidManifest.xml file? If so could you private message me your logcat output and we’ll take a closer look.