首页 文章

GoogleAnalytics android getActivity() . getApplication()无法转换为AnalyticsApplication

提问于
浏览
2

这是我收到错误的代码

@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类

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上的错误

03-09 18:46:11.070 32602-32602 / auc.games2.multigame1 E / AndroidRuntime:FATAL EXCEPTION:main java.lang.ClassCastException:android.app.Application无法转换为auc.games2.Analytics.AnalyticsApplication at auc在Android的android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)的android.app.FragmentManager.Imp.moveToState(FragmentManager.java:843)上的.games2.UI.Fragments.inicial.onViewCreated(inicial.java:127) . app.BackStackRecord.run(BackStackRecord.java:635)位于android.app.Handler的android.app.FragmentManagerImpl.exe上面的android.app.FragmentManagerImpl.execlndingActions(FragmentManager.java:1399)处于android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:426) . handleCallback(Handler.java:615)位于android.app.AtoT.Thread.main上的android.os.Handler.dispatchMessage(Handler.java:92)android.os.Looper.loop(Looper.java:137)处 . (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 $ MethodAn dArgsCaller.run(ZygoteInit.java:1007)位于dalvik.system.NativeStart.main(本地方法)的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)

1 回答

  • 6

    您的 AndroidManifest.xml 文件不包含您的自定义 Application 子类,因此Android使用的是默认值( android.app.Application ) .

    android:name="auc.games2.Analytics.AnalyticsApplication" 属性添加到 <application> 元素 .

相关问题