Android.app.Application无法转换为GoogleAnalyticsApp(libgdx)

时间:2016-04-06 12:35:45

标签: android google-analytics libgdx

我正在尝试将Google Analytics添加到Android上的应用程序(LibGDX)。但我得到了ClassCastException

04-06 15:15:07.359: E/AndroidRuntime(7564): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.mygdx.one4all/com.mygdx.one4all.AndroidLauncher}:
java.lang.ClassCastException: android.app.Application cannot be cast to 
com.mygdx.one4all.AnalyticsApp

当我尝试

@Override
protected void onCreate(Bundle savedInstanceState) { 
//...   
   AnalyticsApp an = (AnalyticsApp) getApplication();
//...

我的AnalyticsApp:

package com.mygdx.one4all;    
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.mygdx.one4all.R.xml;    
import android.app.Application;    
public class AnalyticsApp extends Application {
    private Tracker mTracker; 
    synchronized public Tracker getDefaultTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            mTracker = analytics.newTracker(xml.global_tracker);
        }
        return mTracker;
    }
}

人们建议修改Manifest。但我在那里有我的申请。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mygdx.one4all"
    android:versionCode="9"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.android.vending.BILLING" />

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="23" />

    <application        
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="com.mygdx.one4all.AndroidLauncher"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />        
    </application>

    <application android:name=".AnalyticsApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme">
    </application>

</manifest>

我做错了什么?

1 个答案:

答案 0 :(得分:4)

在你的情况下,你有两个部分,这是错误的。 将其装入一个,并在您的Application类中添加android:name属性。

它应该是这样的:

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/GdxTheme" android:name="com.mygdx.one4all.AnalyticsApp">