Android Question Library creation error, Context.getApplicationContext() error

Carlos marin

Active Member
Licensed User
Longtime User
Hello friends, I would like you to help me with my concerns, I am new to this topic of creating libraries so I try to understand but in the following I have not found a solution.

I am creating a wrapper of an SDK, in the first part I must initialize it and get the ok or the error of said initialization.

The Java code proposed and tested in Android Studio is as follows:

JAVA:
import android.app.Application;
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.attribution.AppsFlyerRequestListener;
// ...
public class AFApplication extends Application {
    // ...
    @Override
    public void onCreate() {
        super.onCreate();
        // ...
        AppsFlyerLib.getInstance().init(<AF_DEV_KEY>, null, this);
        AppsFlyerLib.getInstance().start(getApplicationContext(), <YOUR_DEV_KEY>, new AppsFlyerRequestListener() {
  @Override
  public void onSuccess() {
    Log.d(LOG_TAG, "Launch sent successfully, got 200 response code from server");
  }
 
  @Override
  public void onError(int i, @NonNull String s) {
    Log.d(LOG_TAG, "Launch failed to be sent:\n" +
          "Error code: " + i + "\n"
          + "Error description: " + s);
  }
});
        // ...
    }
    // ...
}

This code tested in android studio works, now when I create the library I do it in Eclipse, download the aar file from the MAVEN repository and extract the jar to use it in the library, I build the code in Eclipse and with the help of SLC created my jar and the XML.

The code created in Eclipse is the following:

JAVA Eclipse:
package anywheresoftware.b4a.sample;

import org.eclipse.jdt.annotation.NonNull;
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.attribution.AppsFlyerRequestListener;
import android.app.Application;
import android.util.Log;
import anywheresoftware.b4a.BA;

@BA.Author("Carlos M")
@BA.Version(1.00F)
@BA.ShortName("appflyer")
@BA.Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE", "android.permission.ACCESS_WIFI_STATE"})

public class appflyer extends Application {

    public void init() {
        
        AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, null, this);
        
        AppsFlyerLib.getInstance().start(BA.applicationContext,<YOUR_DEV_KEY>, new AppsFlyerRequestListener() {
            @Override
            public void onSuccess() {
                Log.d("Exitosa-appflyer", "Launch sent successfully, got 200 response code from server");
            }

            @Override
            public void onError(int i, @NonNull String s) {
                Log.d("Error-flyer", "Launch failed to be sent:\n" +
                        "Error code: " + i + "\n"
                        + "Error description: " + s);
            }
        });
    }
}

I replace the word <YOUR_DEV_KEY> with my development code, I generate the jar and the XML correctly.

When I go to my test project in B4A I load the library, declare it and call it with the class name, in this case appflye.init, but I get an error.

The code I create in B4A is the following:

B4A:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #CustomBuildAction: 1, c:\windows\system32\attrib.exe, +r res\*.* /s
#End Region

#AdditionalJar:af-android-sdk-6.3.2
#AdditionalJar:org.eclipse.jdt.annotation_2.2.600.v20200408-1511

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim appflye As appflyer
'Dim s As
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    appflye.init4
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

and the error I am getting is this:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 34 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:122)
at com.appsflyer.internal.au.<init>:)25)
at com.appsflyer.internal.ae.init:)36844)
at anywheresoftware.b4a.sample.appflyer.init4(appflyer.java:24)
at solodomi.com.main._activity_create(main.java:394)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at solodomi.com.main.afterFirstLayout(main.java:105)
at solodomi.com.main.access$000(main.java:17)
at solodomi.com.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
** Activity (main) Resume **

Apparently it's something with the Context.getApplicationContext(), in the Eclipse code I tried with the getApplicationContext() and it didn't work either, I got the same error, with the BA.applicationContext it didn't work either, maybe I'm passing the context wrong but I don't see clearly how to do it, I can't understand what I'm doing wrong, I searched the forum but I didn't find something that resembled my problem, I appreciate if you can help me understand what I'm doing wrong and what could be a possible solution for this , Thanks a lot.
 
Top