Sonar Cocos 助手 - 在 Android Studio 上设置 Google 玩游戏服务

Sonar Cocos Helper - Setting Up Google Play Game Services on Android Studio

Android Studio 2.3.3(最新)

Cocos2d-x 3.15.1(最新)

I tested on Debug.

这是我第一次使用 Cocos2d-x 游戏引擎,我使用 Cocos2d-x 开发了一个 Android 游戏,一切都很好,但是当我试图显示成就时,它显示了一个 错误 像这样:

java.lang.NullPointerException
                            at sonar.systems.framework.SonarFrameworkFunctions.showAchievements(SonarFrameworkFunctions.java:432)
                            at org.cocos2dx.lib.Cocos2dxRenderer.nativeTouchesEnd(Native Method)
                            at org.cocos2dx.lib.Cocos2dxRenderer.handleActionUp(Cocos2dxRenderer.java:129)
                            at org.cocos2dx.lib.Cocos2dxGLSurfaceView.run(Cocos2dxGLSurfaceView.java:311)
                            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1486)
                            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1263)

我点击显示成就时的代码:

SonarCocosHelper::GooglePlayServices::showAchievements();

当我想登录时:

 java.lang.NullPointerException
           at sonar.systems.framework.SonarFrameworkFunctions.isSignedIn(SonarFrameworkFunctions.java:277)
           at org.cocos2dx.lib.Cocos2dxRenderer.nativeRender(Native Method)
           at org.cocos2dx.lib.Cocos2dxRenderer.onDrawFrame(Cocos2dxRenderer.java:105)
           at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1557)
           at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1263)

我的登录密码:

 if(!SonarCocosHelper::GooglePlayServices::isSignedIn())
    SonarCocosHelper::GooglePlayServices::signIn();

我的清单文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ilyo.test"
    android:installLocation="auto">

    <supports-screens android:anyDensity="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"/>

    <uses-feature android:glEsVersion="0x00020000" />

    <!-- Basic permission for Internet and don't allow turn of the screen -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:label="@string/app_name"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher">

        <!-- Tell Cocos2dxActivity the name of our .so -->
        <meta-data android:name="android.app.lib_name"
                   android:value="MyGame" />

        <!-- Required for Google Play Services -->
<meta-data android:name="com.google.android.gms.games.APP_ID"
    android:value="@string/app_id" />
<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version"/>


<activity
    android:name="org.cocos2dx.cpp.AppActivity"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

</application>
</manifest>

构建Gradle 文件:

Cocos 使用的辅助框架:

我的 Google Play 成就(我从这张照片中删除了 ID):

我已经搜索了与 Android Studio 相关的解决方案,但结果非常少,我不知道解决方案是什么,也不知道这个错误的原因是什么。

谢谢,

我找到了一个解决方案,但现在我想这只是一个肮脏的修复,首先问题出在 GooglePlayServices.java 并且恰好在 onStart 方法:空指针异常,因为 mHelper 为空。

@Override
    public void onStart() 
    {
        mHelper.onStart((Activity) ctx);
    }

让我们进一步了解为什么它会生成 null,所以 :

1. SonarFrameworkFunctions.java

当我们想从 SonarFrameworkFunctions.java 调用 GooglePlayServices 构造函数时:

 //GOOGLE PLAY SERVICES
    if (SonarFrameworkSettings.USE_GOOGLE_PLAY_GAME_SERVICES)
    {
        String packageName = "sonar.systems.frameworks.GooglePlayServices.GooglePlayServices";
        googlePlayServices = (InstantiateFramework(packageName) != null) ? InstantiateFramework(packageName) : new Framework();
    }

2。方法:SonarFrameworkFunctions.java

中的 InstantiateFramework
public static Framework InstantiateFramework(String packageName)
{
    Framework tmp = null;
    try
    {
        tmp = (Framework) Class.forName(packageName).getConstructor().newInstance();
        tmp.SetActivity(((SonarFrameworkActivity) app));
    } catch (InstantiationException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e)
    {
        e.printStackTrace();
        Log.e("Class not found", "class doesn't exist or bad package name " + packageName);
    }
    return tmp;
}

构造函数为空,因此只有一个名为 onCreate 的方法实例化并准备 mHelper 以访问 Google Play 服务的所有功能。

解决方案(脏修复):

GooglePlayServices.java

public GooglePlayServices() 
    {
       mHelper=getGameHelper();
    mHelper.setup(this);
    }