ClassCastException导致应用程序停止

时间:2017-11-17 15:16:56

标签: java android

我很难解决这个问题,我已经在搜索一些类似的问题了,但我没有这种经验, 从我的logcat我发现了这个:

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.dot.analyticsdemo.DemoApp
                                                                                   at com.example.dot.analyticsdemo.MainActivity.onCreate(MainActivity.java:23)

这是我的MainActivity:

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

        DemoApp application = (DemoApp) getApplication();
        mTracker = application.getDefaultTracker();

        // starts a new session and notification to google analytics
        mTracker.send(new HitBuilders.ScreenViewBuilder()
                .setNewSession()
                .build());
    }

第23行是:DemoApp application = (DemoApp) getApplication();

这是DemoApp:

package com.example.dot.analyticsdemo;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

public class DemoApp extends Application {

    private Tracker mTracker;

    /**
     * Gets the default {@link Tracker} for this {@link Application}.
     * @return tracker
     */
    synchronized public Tracker getDefaultTracker() {

        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);

            // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
            mTracker = analytics.newTracker(R.xml.global_tracker);
            mTracker.enableExceptionReporting(true);
            mTracker.enableAutoActivityTracking(true);
        }

        return mTracker;
    }
}

我的错误是什么?

1 个答案:

答案 0 :(得分:0)

您是否在清单文件中声明了应用程序名称?

@Scheduled(fixedRateString = "#{@mySchedulerRate}")
public void runScheduledUpdate() {
   ...
{