0

I am using Buildbox. I defined a custom openURL method in Buildbox. I am trying to track worlds with events. How can I reach non-static mFirebaseAnalytics instance in static method?

package com.secrethq.utils;

import java.lang.ref.WeakReference;
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;
import java.nio.IntBuffer;

import com.google.firebase.analytics.FirebaseAnalytics;

import android.app.Activity;


public class PTServicesBridge extends Activity
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private FirebaseAnalytics mFirebaseAnalytics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
    }


    public static void openUrl( String url ){
        Log.v(TAG, "PTServicesBridge  -- Open URL " + url);

        PTServicesBridge.urlString = url;

        Bundle params = new Bundle();
        params.putString("levelname", url);
        mFirebaseAnalytics.logEvent("levels", params); //Problematic line it gives error.



    }


}

Problematic line:

 mFirebaseAnalytics.logEvent("levels", params); //Problematic line it gives error.
gurkan stack
  • 413
  • 1
  • 15
  • 43
  • you can not access non-static variable from a static method. remove static keyword from openUrl method. – Dhaval Patel Dec 19 '19 at 18:33
  • if I remove this method is not working anyway I should access it. – gurkan stack Dec 19 '19 at 18:34
  • What do you mean by not working? – Dhaval Patel Dec 19 '19 at 18:35
  • Code does not enter here: Log.v(TAG, "PTServicesBridge -- Open URL " + url); Not working these lines. It works if only I make it static – gurkan stack Dec 19 '19 at 18:36
  • Are you accessing openUrl method from other class? – Dhaval Patel Dec 19 '19 at 18:37
  • Buildbox trigger this method when user click open URL button in the game. So I do not access, buildbox access and run it. If I remove static from this method, buildbox never trigger it button not working. If I make it static so no problem, method is triggered. – gurkan stack Dec 19 '19 at 18:39
  • @gurkanstack, there must be something you're missing here. Apps such as this generally highly discourage using `static` keyword in general. The right solution **is** to remove the `static`, and then figure out why buildox can't find your method (most likely you aren't referencing it correctly, but I can't see your screen). – M. Prokhorov Dec 19 '19 at 18:42

1 Answers1

1

I just dig up the buildbox code, Here is how you can achieve your task!

Use PTServicesBridge.s_activity to get FirebaseAnalytics instance inside the method.

public static void openUrl( String url ){
        Log.v(TAG, "PTServicesBridge  -- Open URL " + url);

        PTServicesBridge.urlString = url;

        Bundle params = new Bundle();
        params.putString("levelname", url);

        // Get FirebaseAnalytics Object
        FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(PTServicesBridge.activity);
        mFirebaseAnalytics.logEvent("levels", params); //Problematic line it gives error.
}
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46