问题
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();
}
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;
}
}
this is the error on logcat
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)
回答1:
Your AndroidManifest.xml
file does not contain your custom Application
subclass, so Android is using the default one (android.app.Application
).
Add an android:name="auc.games2.Analytics.AnalyticsApplication"
attribute to your <application>
element.
来源:https://stackoverflow.com/questions/35905587/googleanalytics-android-getactivity-getapplication-cannot-cast-to-analyticsa