错误:(51,9)错误:无法找到符号变量mTracker Google Analytics

时间:2015-12-07 01:23:10

标签: java android google-analytics analytics

您好我尝试将谷歌分析放入我的应用程序中,但是我看到文档但不适合我,我有这个错误错误:(51,9)错误:找不到符号变量mTracker:

AnalyticsApplication.java

public class AnalyticsApplication extends Application {
private Tracker mTracker;

/**
 * Gets the default {@link Tracker} for this {@link Application}.
 * @return tracker
 */
synchronized public Tracker getDefaultTracker() {
    if (mTracker == null) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
        mTracker = analytics.newTracker(R.xml.global_tracker);
    }
    return mTracker;
}

清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


<application
    .....
</application>

MainActivity

public class MainActivity extends Activity {

int n;
String adc;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // Obtain the shared Tracker instance.
    MYPACKAGE.AnalyticsApplication application = (MYPACKAGE.AnalyticsApplication) getApplication();
    mTracker = application.getDefaultTracker();

...

错误: 错误:(51,9)错误:找不到符号变量mTracker

FIXED: 问题是我需要把它放在MainActivity中:

公共类MainActivity扩展了Activity {

int n;
String adc;
**private Tracker mTracker;**
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // Obtain the shared Tracker instance.

2 个答案:

答案 0 :(得分:0)

您的MainActivity类正在使用字段mTracker,该字段在应用程序中定义。

建议您更改代码:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // Obtain the shared Tracker instance.
    MYPACKAGE.AnalyticsApplication application = (MYPACKAGE.AnalyticsApplication) getApplication();
    Tracker tracker = application.getDefaultTracker(); // Changed.

答案 1 :(得分:0)

将此添加到您的清单中:

<application
name = "yourpackagename.applicationclassname"
    .....
</application>

试试这个,这将有效