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);
}
}
所以现在当我运行此选项并从菜单中选择选项时,它会出错并导致应用程序崩溃。
错误日志:
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$3.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
中选择了这个例子那怎么能解决这个错误呢?
答案 0 :(得分:1)
如果您刚刚定义了MyApp,请确保您已在AndroidManifest.xml的name属性中添加了MyApp类,如下所示:
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/AppTheme">
&#13;
否则,您的Application类的onCreate()不会被调用,并且跟踪器仍然为null。