Monday, 1 February 2021

google api error - not able to get the google sign in to work

I am working on a React Native app.
I am using "ReactContextBaseJavaModule" to connect my JS and Java files.
I am trying to access the Fitness API. So to do that it has to login to a google account.
I have followed all the steps: created a ReactModule, ReactNativePackager, added the package to my MainApplication file.
I have even tried to invoke a simple "HelloWorld" function and it works!!! my React bridge is connected perfectly.

Now I call this function "fitSignIn" from my ReactModule. The code for fitSignIn is in my MainActivity(). Code shown below for both ReactModule and MainACtivity.

class ReactModule internal constructor(reactContext: ReactApplicationContext?) : ReactContextBaseJavaModule(reactContext) {

 @ReactMethod
    fun getAuthorizationAndReadData() {
        try {
            MainActivity().fitSignIn(FitActionRequestCode.INSERT_AND_READ_DATA, reactApplicationContext)
        } catch (e: Exception) {
            Log.i("error", "error!!!!")
        }
    }
}

MainACtivity()

class MainActivity : ReactActivity() {
  fun fitSignIn(requestCode: FitActionRequestCode, context: Context) {
           oAuthPermissionsApproved()
}

private val fitnessOptions: FitnessOptions by lazy {
        FitnessOptions.builder()
                .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
                .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
                .build()
    }

 private fun oAuthPermissionsApproved() = GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions)

 private fun getGoogleAccount() = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
}

When I debugged this the code fails at getGoogleAccount with "java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference" error.

My guess is "this" in the function isn't working.

Programming gurus please help me fix this!!



from google api error - not able to get the google sign in to work

No comments:

Post a Comment