Tracker 发送方法不适用于 google 分析

Trakcer send method not working for google analytics

Mainactivity.java:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_settings:
            MyApp.tracker().send(
                    new HitBuilders.EventBuilder("ui", "open").setLabel(
                            "settings").build());

            return false;
        default:
            return super.onOptionsItemSelected(item);
        }

    }
}

Myapp.java:

public class MyApp extends Application {
    private static GoogleAnalytics analytics;
    private static Tracker tracker;
    public static GoogleAnalytics analytics() {
        return analytics;
    }

    public static Tracker tracker() {
        return tracker;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        analytics = GoogleAnalytics.getInstance(this);

        tracker = analytics.newTracker("my tracker id");

        tracker.enableExceptionReporting(true);

        tracker.enableAdvertisingIdCollection(true);


        tracker.enableAutoActivityTracking(true);
    }
    }

所以现在当我 运行 这个和当时菜单中的 select 选项时,它会出错并使应用程序崩溃。

错误日志:

  FATAL EXCEPTION: main

  java.lang.NullPointerException
    at com.asf.gatest.MainActivity.onOptionsItemSelected(MainActivity.java:35)
    at android.app.Activity.onMenuItemSelected(Activity.java:2650)
    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
    at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
    at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
    at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:538)
    at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
    at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
    at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
    at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
    at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:187)
    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
    at android.widget.AbsListView.run(AbsListView.java:3865)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5333)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)

我从 https://github.com/googleanalytics/hello-world-android-app

中拿了这个例子

那么如何解决这个错误呢?

如果您刚刚定义了 MyApp,请确保您已将 MyApp class 添加到 AndroidManifest.xml 的名称属性中,如下所示:

 <application
        android:name=".MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

否则您的应用程序 class 的 onCreate() 将不会被调用并且跟踪器仍将为空。