在使用LibGDX和BaseGameUtils创建gameHelper时如何修复空指针异常?

时间:2017-06-08 21:01:59

标签: java android libgdx google-play-services

我目前正在制作我的第一个游戏(我不会无耻地推广),但我正在尝试设置排行榜,但是当我尝试初始化我的GameHelper时,我得到了一个例外。我无法找到任何答案,我将不胜感激。

代码:

package com.aidanstrong.runner;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;

public class AndroidLauncher extends AndroidApplication implements GameHelperListener, PlayServices{

private final static int requestCode = 1;
private GameHelper gameHelper;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    initialize(new MyGame(this), config);
    gameHelper = new GameHelper(this,GameHelper.CLIENT_ALL);
    gameHelper.enableDebugLog(true);
    gameHelper.setup(this);
}

@Override
public void onStart(){
    super.onStart();
    gameHelper.onStart(this);
}

@Override
public void onStop(){
    super.onStop();
    gameHelper.onStop();
}

@Override
public void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);
    gameHelper.onActivityResult(request, response, data);
}

@Override
public boolean signIn() {
    return gameHelper.isSignedIn();
}

@Override
public void logIn() {
    try {
        runOnUiThread(new Runnable(){
            public void run() {
                gameHelper.beginUserInitiatedSignIn();
            }
        });
    } catch (final Exception ex) {
    }
}

@Override
public void submitScore(int score) {
    Games.Leaderboards.submitScore(gameHelper.getApiClient(),"CgkIlKy-68IeEAIQAQ",score);
}


@Override
public void getLeaderboard() {
    Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(),"CgkIlKy-68IeEAIQAQ");
}

@Override
public void onSignInFailed() {
}

@Override
public void onSignInSucceeded() {
}
}

我收到的错误是

  

" java.lang.RuntimeException:无法实例化活动   ComponentInfo {com.aidanstrong.runner / com.aidanstrong.runner.AndroidLauncher}:   java.lang.NullPointerException:尝试调用虚方法   ' android.content.Context   android.content.Context.getApplicationContext()'在null对象上   参考"在第21行。

编辑: 我试了一下并得到了一个新错误,将构造函数中的所有语句移到gameHelper.setup之前(这个)我得到了错误

  

致命的例外:主要                                                                           过程:com.aidanstrong.runner,PID:11950                                                                           java.lang.NoSuchMethodError:没有直接的方法   (Ljava /郎/字符串; LCOM /谷歌/机器人/克/普通/ API /阿比$ ZZA; LCOM /谷歌/机器人/克/普通/ API /阿比$ ZZC;)V   在课堂Lcom / google / android / gms / common / api / Api;或者它的超级课程   (声明&com; go.google.android.gms.common.api.Api'出现在   /data/app/com.aidanstrong.runner-1/split_lib_dependencies_apk.apk)                                                                               在com.google.android.gms.plus.Plus。(未知来源)                                                                               在   com.google.example.games.basegameutils.GameHelper.createApiClientBuilder(GameHelper.java:276)                                                                               在   com.google.example.games.basegameutils.GameHelper.setup(GameHelper.java:309)                                                                               在   com.aidanstrong.runner.AndroidLauncher.onCreate(AndroidLauncher.java:32)                                                                               在android.app.Activity.performCreate(Activity.java:6270)                                                                               在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)                                                                               在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)                                                                               在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)                                                                               在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                               在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1363)                                                                               在android.os.Handler.dispatchMessage(Handler.java:102)                                                                               在android.os.Looper.loop(Looper.java:148)                                                                               在android.app.ActivityThread.main(ActivityThread.java:5491)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)'

    <?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aidanstrong.runner"
    android:versionCode="6"
    android:versionName="1.22" >
   <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="25" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE"/>


    <application

        android:allowBackup="true"
       android:icon="@drawable/secondicon"
        android:label="Jump Man"
        android:theme="@style/GdxTheme" >
        <activity
        android:name="com.aidanstrong.runner.AndroidLauncher"
        android:label="Jump Man"
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" />
    <meta-data android:name="com.google.android.gms.appstate.APP_ID" android:value="@string/app_id" />
</application>

1 个答案:

答案 0 :(得分:0)

将元数据保留在<application代码

<meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" />
<meta-data android:name="com.google.android.gms.appstate.APP_ID" android:value="@string/app_id" />

删除AndroidLauncher

的默认构造函数
public class AndroidLauncher extends AndroidApplication implements IGoogleServices {

private GameHelper gameHelper;
private final static int REQUEST_CODE_UNUSED = 9002;
private static final String TAG = "[" + AndroidLauncher.class.getSimpleName() + "]";

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
    gameHelper.enableDebugLog(true);
    GameHelper.GameHelperListener gameHelperListener = new GameHelper.GameHelperListener() {
        @Override
        public void onSignInSucceeded(){}

        @Override
        public void onSignInFailed() {}
    };

    //gameHelper.setMaxAutoSignInAttempts(0);
    gameHelper.setup(gameHelperListener);

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

    initialize(new MyGame(this), config);
}

@Override
protected void onStart() {
    super.onStart();
    gameHelper.onStart(this);
}

@Override
protected void onStop() {
    super.onStop();
    gameHelper.onStop();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    gameHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void signIn() {
    try {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                gameHelper.beginUserInitiatedSignIn();
            }
        });
    }
    catch (Exception e) {
        Gdx.app.log("MainActivity", "Log in failed: " + e.getMessage() + ".");
    }
}

 @Override
public void signOut() {
    try {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //gameHelper.signOut();
            }
        });
    }
    catch (Exception e) {
        Gdx.app.log("MainActivity", "Log out failed: " + e.getMessage() + ".");
    }
}

 @Override
public void submitScore(long score) {
    if (isSignedIn()) Games.Leaderboards.submitScore(gameHelper.getApiClient(), getString(R.string.leaderboard_id), score);
}

@Override
public void showScores() {
    if(isSignedIn())
        startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), getString(R.string.leaderboard_id)), REQUEST_CODE_UNUSED);
    else
        signIn();
}

@Override
public boolean isSignedIn() {
    return gameHelper.isSignedIn();
}

@Override
public void start() {
    gameHelper.onStart(this);
}

IGoogleServices是core模块中的一个接口,用于与android模块接口。