這個,我得到了錯誤的代碼Google分析的Android getActivity()。getApplication()無法投射到AnalyticsApplication
@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 EX CEPTION:主要 java.lang.ClassCastException:android.app.Application不能auc.games2.UI.Fragments.inicial.onViewCreated施放 到auc.games2.Analytics.AnalyticsApplication (inicial.java:127) 在 android.app.FragmentManagerImpl.moveToState在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)(FragmentManager.java:843) 在android.app.BackStackRecord.run(BackStackRecord.java:635) 在 android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399) at android.app.FragmentManagerIm pl $ 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 d alvik.system.NativeStart.main(本機方法)