0

I got an exception:

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to analytics.AnalyticsApplication

This is my MainActivity

public class MainActivity extends AppCompatActivity {

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


        AnalyticsApplication application = (AnalyticsApplication) getApplication();
        mtracker = application.getDefaultTracker();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mtracker.setScreenName("Main Activity");
        mtracker.send(new HitBuilders.ScreenViewBuilder().build());
    }
}
2
  • Application cannot be cast to AnalyticsApplication Commented Jan 17, 2017 at 15:25
  • You're most likely not setting the application in the Manifest file. Commented Jan 17, 2017 at 15:25

1 Answer 1

2

It's look like, you are missing binding part in Manifest. Look here for name attribute.

When you are creating subclass of Application then you must specify that class name in your manifest file.

<application
    android:name=".AnalyticsApplication"
    ... />

is equal to:

<application
    android:name="com.yournamespace.AnalyticsApplication"
    ... />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.