ClassCastException: com.soomla.SoomlaApp 无法转换为 org.cocos2dx.cpp.AnalyticsApplication

ClassCastException: com.soomla.SoomlaApp cannot be cast to org.cocos2dx.cpp.AnalyticsApplication

我想实施 Google-Analytics 和 Soomla Store 以进行应用内购买。问题是,每次我想跟踪屏幕时,我的应用程序都会崩溃 Android。在 iOS 下,它就像一个魅力。

崩溃原因:

java.lang.ClassCastException: com.soomla.SoomlaApp cannot be cast to org.cocos2dx.cpp.AnalyticsApplication

我认为 AndroidManifest.xml 中有错误。我是 Android 的新手,但我在 iOS 开发方面经验丰富。

当我在我的 C++ 代码中调用 GoogleAnalyticsTracker::sendScreen("Test Screen") 时发生崩溃。

GoogleAnalyticsTracker.cpp的一部分:

void GoogleAnalyticsTracker::sendScreen(const char* screenName)
{
    sendScreenJNI(screenName);
}

GoogleAnalyticsTracker_JNI.cpp的一部分

#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include "GoogleAnalyticsTracker_JNI.h"
#define CLASS_NAME "org/cocos2dx/cpp/AppActivity"

void sendScreenJNI(const char* screenName) {
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "sendScreen", "(Ljava/lang/String;)V"))
    {
        jstring jScreenName = t.env->NewStringUTF(screenName);
        t.env->CallStaticVoidMethod(t.classID, t.methodID, jScreenName);
        t.env->DeleteLocalRef(jScreenName);
        t.env->DeleteLocalRef(t.classID);
    }
}

我的一部分 AndroidManifest.xml:

    <application android:name="com.soomla.SoomlaApp" android:label="@string/app_name"
             android:icon="@drawable/icon">
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    <!-- Tell Cocos2dxActivity the name of our .so -->
    <meta-data android:name="android.app.lib_name"
              android:value="cocos2dcpp" />
    <activity android:name="org.cocos2dx.cpp.AppActivity"
              android:label="@string/app_name"
              android:screenOrientation="landscape"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:configChanges="orientation">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="org.cocos2dx.cpp.AnalyticsApplication"/>
    <activity android:name="com.soomla.store.billing.google.GooglePlayIabService$IabActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
    <meta-data android:name="billing.service" android:value="google.GooglePlayIabService"/>
</application>

这是 AnalyticsApplication.java

的代码
package org.cocos2dx.cpp;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import ch.steeve.game.R;

import android.app.Application;

public class AnalyticsApplication extends Application {
    Tracker mTracker;
    synchronized Tracker getTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            mTracker = analytics.newTracker(R.xml.ga_tracker);
        }
        return mTracker;
    }
}

AppActivity.java的一部分

public static void sendScreen(String screenName) {
    Tracker t = ((AnalyticsApplication) Cocos2dxActivity.getContext().getApplicationContext()).getTracker();
    t.setScreenName(screenName);
    t.send(new HitBuilders.AppViewBuilder().build());
}
  1. AndroidManifest 中的这个字符串不正确:

    activity android:name="org.cocos2dx.cpp.AnalyticsApplication"

    AnalyticsApplication 不是 activity,它是应用程序。删除此字符串。

  2. 您的 AnalyticsApplication 应该扩展 com.soomla.SoomlaApp

  3. 在 AndroidManifest 中替换字符串:

    申请android:name="com.soomla.SoomlaApp"android:label="@string/app_name" android:icon="@drawable/icon"

    申请android:name="org.cocos2dx.cpp.AnalyticsApplication"android:label="@string/app_name" android:icon="@drawable/icon"