hydrasdk-demo-android
hydrasdk-demo-android copied to clipboard
NullPointerException: Cannot find initialized sdk instance with name [vpnAccess]
This Exception happened with me with some devices and some devices not happen, just when i trying to open the app it throw this Exception, It's working fine with most devices but with some devices it throw this Exception!
2020-05-26 20:52:04.856 10761-10761/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rket.reocketvpn, PID: 10761
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rket.reocketvpn/com.rket.reocketvpn.ui.activity.splash.SplashActivity}: java.lang.NullPointerException: Cannot find initialized sdk instance with name [vpnAccess]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.NullPointerException: Cannot find initialized sdk instance with name [vpnAccess]
at i.b.h.c.a.a(ObjectHelper.java:1)
at i.b.h.c.a.b(ObjectHelper.java:2)
at com.anchorfree.sdk.p5.a(UnifiedSDK.java:19)
at com.anchorfree.sdk.p5.a(UnifiedSDK.java:20)
at com.rket.reocketvpn.ui.activity.splash.SplashActivity.a(SplashActivity.kt:2)
at i.g.a.c.a.a.onCreate(BaseActivity.kt:11)
at android.app.Activity.performCreate(Activity.java:7458)
at android.app.Activity.performCreate(Activity.java:7448)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
class SplashActivity : BaseActivity() {
override fun bindView(savedInstanceState: Bundle?) {
if (UnifiedSDK.getInstance().backend.isLoggedIn) {
Handler().postDelayed({
startActivity(Intent(this@SplashActivity, MainActivity::class.java))
finishAffinity()
}, 1000)
} else {
login()
}
}
private fun login() {
val hostUrl = BuildConfig.BASE_HOST
val carrierId = BuildConfig.BASE_CARRIER_ID
(application as MainApplication).setNewHostAndCarrier(hostUrl, carrierId)
val authMethod = AuthMethod.anonymous()
UnifiedSDK.getInstance().backend.login(authMethod, object : Callback<User?> {
override fun success(user: User) {
startActivity(Intent(this@SplashActivity, MainActivity::class.java))
finish()
}
override fun failure(e: VpnException) {
//
}
})
}
}
It happens with this device
Hello! Where do you initialize the sdk instance with UnifiedSDK.getInstance(ClientInfo)?
@timoshenkoav in the Application class
class MainApplication : Application() {
private var unifiedSDK: UnifiedSDK? = null
companion object {
const val CHANNEL_ID = "vpn"
var selectedCountry = UnifiedSDK.COUNTRY_OPTIMAL
private var mInstance: MainApplication? = null
@Synchronized
fun getInstance(): MainApplication {
if (mInstance == null) mInstance = MainApplication()
return mInstance!!
}
}
private val prefs: SharedPreferences
get() = getSharedPreferences(BuildConfig.SHARED_PREFS, Context.MODE_PRIVATE)
override fun onCreate() {
super.onCreate()
mInstance = this
initHydraSdk()
}
private fun initHydraSdk() {
createNotificationChannel()
val clientInfo = ClientInfo.newBuilder()
.baseUrl(prefs.getString(BuildConfig.STORED_HOST_URL_KEY, BuildConfig.BASE_HOST)!!)
.carrierId(prefs.getString(BuildConfig.STORED_CARRIER_ID_KEY, BuildConfig.BASE_CARRIER_ID)!!)
.build()
val transportConfigList: MutableList<TransportConfig> = ArrayList()
transportConfigList.add(HydraTransportConfig.create())
transportConfigList.add(OpenVpnTransportConfig.tcp())
transportConfigList.add(OpenVpnTransportConfig.udp())
UnifiedSDK.update(transportConfigList, CompletableCallback.EMPTY)
val config = UnifiedSDKConfig.newBuilder().idfaEnabled(false).build()
unifiedSDK = UnifiedSDK.getInstance(clientInfo, config)
val notificationConfig = NotificationConfig.newBuilder()
.title(resources.getString(R.string.app_name))
.channelId(CHANNEL_ID)
.build()
UnifiedSDK.update(notificationConfig)
UnifiedSDK.setLoggingLevel(Log.VERBOSE)
}
fun setNewHostAndCarrier(hostUrl: String?, carrierId: String?) {
if (TextUtils.isEmpty(hostUrl)) {
prefs.edit().remove(BuildConfig.STORED_HOST_URL_KEY).apply()
} else {
prefs.edit().putString(BuildConfig.STORED_HOST_URL_KEY, hostUrl).apply()
}
if (TextUtils.isEmpty(carrierId)) {
prefs.edit().remove(BuildConfig.STORED_CARRIER_ID_KEY).apply()
} else {
prefs.edit().putString(BuildConfig.STORED_CARRIER_ID_KEY, carrierId).apply()
}
initHydraSdk()
}
}
ok, everything seems fine. i will investigate the issue, for now you can try to switch and use UnifiedSDK instance you have in your application. instead of using UnifiedSDK.getInstance()
@timoshenkoav Ok, Thank you
@timoshenkoav can you please explain more about (UnifiedSDK instance you have in application. ?? )