这是我收到错误的代码
@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: 致命异议:主要 java.lang.ClassCastException:无法转换android.app.Application 到auc.games2.Analytics.AnalyticsApplication at auc.games2.UI.Fragments.inicial.onViewCreated(inicial.java:127) 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843) 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 在android.app.BackStackRecord.run(BackStackRecord.java:635) 在 android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399) 在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:426) 在android.os.Handler.handleCallback(Handler.java:615) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4867) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1007) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:6)
您的AndroidManifest.xml
文件不包含自定义Application
子类,因此Android使用默认值(android.app.Application
)。
在android:name="auc.games2.Analytics.AnalyticsApplication"
元素中添加<application>
属性。