无法从静态上下文中引用非静态字段 mFirebaseAnalytics

Non-static field mFirebaseAnalytics can not be referenced from a static context

我正在使用 Buildbox。我在 Buildbox 中定义了一个自定义的 openURL 方法。我正在尝试通过事件跟踪世界。如何在静态方法中访问非静态 mFirebaseAnalytics 实例?

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.



    }


}

问题行:

 mFirebaseAnalytics.logEvent("levels", params); //Problematic line it gives error.

我刚刚挖掘了 buildbox 代码,这是您完成任务的方法!

使用 PTServicesBridge.s_activity 在方法中获取 FirebaseAnalytics 实例。

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.
}