Google Analytics(V4)OnCreate无法从应用中投射

时间:2014-05-25 19:10:39

标签: analytics

我已经按照比谷歌更好的教程开始在我的应用中获得分析。 问题是

package com.sgdva.ishikawa;
import com.google.android.gms.analytics.Tracker;
import com.google.android.gms.analytics.GoogleAnalytics;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.util.HashMap;

public class Ishikawa extends Activity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.sources, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.action_settings:
        startActivity(new Intent(Ishikawa.this, Sources.class));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-50596309-1";

// Logging TAG
private static final String TAG = "Ishikawa";

public static int GENERAL_TRACKER = 0;

public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.
    GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg:
                    // roll-up tracking.
    ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a
                        // company.
}

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

public Ishikawa() {
    super();
}

synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics
                .newTracker(R.xml.app_tracker)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics
                        .newTracker(PROPERTY_ID) : analytics
                        .newTracker(R.xml.ecommerce_tracker);
        mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get a Tracker (should auto-report)
    ((Ishikawa) getApplication())
    .getTracker(Ishikawa.TrackerName.APP_TRACKER);

    setContentView(R.layout.activity_ishikawa);

在这一行

((Ishikawa) getApplication()).getTracker(Ishikawa.TrackerName.APP_TRACKER); 它说:不能从应用程序转换为Ishikawa 我已经读过这是因为我的android名称没有在我的manifest.xml中显示,但它是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sgdva.ishikawa"
android:versionCode="2"
android:versionName="1.1" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:name="com.sgdva.ishikawa.Ishikawa"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" 
>
<!-- Google Analytics Version v4 needs this value for easy tracking -->
<meta-data android:name="com.google.android.gms.analytics.globalConfigResource"
android:resource="@xml/global_tracker" />        
<activity
android:name="com.sgdva.ishikawa.Ishikawa"
android:label="@string/app_name" 
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sgdva.ishikawa.Machine"
android:label="@string/title_activity_machine"
android:screenOrientation="portrait" >
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|s    mallestScreenSize"/>
<meta-data android:name="com.google.android.gms.version"
                   android:value="@integer/google_play_services_version" />
<activity
        android:name="com.sgdva.ishikawa.MainActivity"
        android:label="@string/title_activity_main" 
        android:screenOrientation="portrait">
    </activity>
</application>
</manifest>

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

这是我的坏事,我正在调用我的&#34;活动&#34;作为一个&#34;应用程序&#34; ((Ishikawa) getApplication()).getTracker(Ishikawa.TrackerName.APP_TRACKER); 我创建了另一个.class,名为&#34; Tracker.java&#34;获取分析代码并改为引用它:

((Tracker) getApplication()).getTracker(Tracker.TrackerName.APP_TRACKER);