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

时间:2015-02-02 23:19:25

标签: android google-analytics crash cocos2d-x soomla

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

坠机原因:

  

java.lang.ClassCastException:com.soomla.SoomlaApp无法强制转换为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 个答案:

答案 0 :(得分:1)

  1. AndroidManifest中的此字符串不正确:

    活动android:name =“org.cocos2dx.cpp.AnalyticsApplication”

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

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

  3. 在AndroidManifest中替换字符串:

    应用程序android:name =“com.soomla.SoomlaApp”android:label =“@ string / app_name”                  机器人:图标= “@绘制/图标”

    应用程序android:name =“org.cocos2dx.cpp.AnalyticsApplication”android:label =“@ string / app_name”              机器人:图标= “@绘制/图标”