GoogleAnalytics android getActivity()。getApplication()不能转换为AnalyticsApplication [英] GoogleAnalytics android getActivity().getApplication() cannot cast to AnalyticsApplication

查看:176
本文介绍了GoogleAnalytics android getActivity()。getApplication()不能转换为AnalyticsApplication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我得到错误的代码

this the code where I got the error

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Obtain the shared Tracker instance.
        AnalyticsApplication application = (AnalyticsApplication) getActivity().getApplication();
        mTracker = application.getDefaultTracker();
}

这是我的AnalyticsApplication类

this is my AnalyticsApplication class

package auc.games2.Analytics;
/*
 * Copyright Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


        import android.app.Application;

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

        import auc.games2.R;

/**
 * This is a subclass of {@link Application} used to provide shared objects for this app, such as
 * the {@link Tracker}.
 */
public class AnalyticsApplication 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);
        }
        return mTracker;
    }
}

这是logcat上的错误

this is the error on logcat


03-09 18:46:11.070 32602-32602 / auc.games2.multigame1 E / AndroidRuntime:
致命例外:main
java.lang.ClassCastException:android.app.Application不能转换为
到auc.games2.Analytics.AnalyticsApplication
在auc.games2.UI.Fragments.inicial.onViewCreated(ini​​cial.java:127)
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843)
at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRec ord.run(BackStackRecord.java:635)
at
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
at android.app.FragmentManagerImpl $ 1.run(FragmentManager.java :426)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os。 Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Me thod.invokeNative(本地方法)
在java.lang.reflect.Method.invoke(Method.java:511)
at
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)

03-09 18:46:11.070 32602-32602/auc.games2.multigame1 E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.app.Application cannot be cast to auc.games2.Analytics.AnalyticsApplication at auc.games2.UI.Fragments.inicial.onViewCreated(inicial.java:127) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) at android.app.BackStackRecord.run(BackStackRecord.java:635) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4867) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) at dalvik.system.NativeStart.main(Native Method)


推荐答案

您的 AndroidManifest.xml 文件不包含你的自定义 Application 子类,所以Android使用默认的( android.app.Application )。

Your AndroidManifest.xml file does not contain your custom Application subclass, so Android is using the default one (android.app.Application).

android:name =auc.games2.Analytics.AnalyticsApplication属性添加到<应用程ation> 元素。

这篇关于GoogleAnalytics android getActivity()。getApplication()不能转换为AnalyticsApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆