GoogleApiClient空异常问题

时间:2018-07-23 16:01:51

标签: android nullpointerexception google-play-services

我正在尝试使用Google Play排行榜和成就。 因为我想在不同的地方使用Api,所以我有一个用于连接的BaseActivity,并像这样构建对象apiClient

public void onClick(View view) {

        mCountDown.cancel();
        if (index < totalQuestion) //Still Questions in list
        {
            Button clickedButton = (Button) view;
            if (clickedButton.getText().equals(Common.questionList.get(index).getCorrectAnswer())) {
                //Chose correct answer
                MediaPlayer correct_sound = MediaPlayer.create(PlayingActivity.this,R.raw.correct);
                correct_sound.start();
                score += 10;

                if(score>100) Games.Achievements
                        .unlock(apiClient,
                                getString(R.string.achievement_hey_thats_easy));

                correctAnswer++;

                showQuestion(++index); //next question
            }

此后,我希望e.x在此类其他活动中使用api来实现成就

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: eu.healthydev.quizhero, PID: 27060
                  java.lang.NullPointerException: GoogleApiClient must not be null
                      at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
                      at com.google.android.gms.internal.zzaad$zza.<init>(Unknown Source)
                      at com.google.android.gms.games.Games$BaseGamesApiMethodImpl.<init>(Unknown Source)
                      at com.google.android.gms.games.internal.api.AchievementsImpl$UpdateImpl.<init>(Unknown Source)
                      at com.google.android.gms.games.internal.api.AchievementsImpl$4.<init>(Unknown Source)
                      at com.google.android.gms.games.internal.api.AchievementsImpl.unlock(Unknown Source)
                      at eu.healthydev.quizhero.PlayingActivity.onClick(PlayingActivity.java:101)
                      at android.view.View.performClick(View.java:4771)
                      at android.view.View$PerformClick.run(View.java:19771)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5311)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

但是我得到apiclient的Null异常错误。这是我的日志:

public interface IPrimary
{
    int Id { get; set; }
}

public partial class Education : IPrimary { }

如果我所有的代码都在一个活动中,那么一切正常,但是我不希望连接和Google弹出窗口在我的游戏活动中再次显示。我希望该连接成为“家庭”活动,并使用apiClient对象作为我的“玩”活动。还是我不明白的其他错误。我会提供任何帮助!

0 个答案:

没有答案