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からこの例を取り上げました
では、このエラーをどのように解決できますか?