【问题标题】:NullPointerException when using Cloud Save in Android在 Android 中使用云保存时出现 NullPointerException
【发布时间】:2013-06-29 23:06:02
【问题描述】:

我需要初始化 AppStateClient 以使用 Cloud 来保存和加载数据。接下来我做:

public Constructor() {
(1) private GameHelper aHelper = new GameHelper(this);    
(2) aHelper.setup(this, GameHelper.CLIENT_APPSTATE | GameHelper.CLIENT_GAMES);
(3) private AppStateClient mHelper = aHelper.getAppStateClient();
}

但在第二个字符串中,我总是得到 NullPointerException。在 Goggle 的 GameHelper 类中,它崩溃了:

 mGamesClient = new GamesClient.Builder(getContext(), this, this)
                    .setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL)
                    .setScopes(mScopes)
                    .create();
and on:  

mAppStateClient = new AppStateClient.Builder(getContext(), this, this)
                    .setScopes(mScopes)
                    .create();

我做错了什么?

aHelper 变量我在我的应用程序中也用于成就和排行榜,并且一切正常。

【问题讨论】:

  • 你试过调试吗?

标签: java android cloud achievements leaderboard


【解决方案1】:

问题已解决。

public Constructor() {
 private GameHelper aHelper = new GameHelper(this);    
 aHelper.setup(this, GameHelper.CLIENT_APPSTATE | GameHelper.CLIENT_GAMES);
 private AppStateClient mHelper = aHelper.getAppStateClient();
}

此代码需要调用 onCreate(Bundle savedInstanceState) 而不是类的构造函数

@Override
  public void onCreate(Bundle savedInstanceState) {
private GameHelper aHelper = new GameHelper(this);    
     aHelper.setup(this, GameHelper.CLIENT_APPSTATE | GameHelper.CLIENT_GAMES);
     private AppStateClient mHelper = aHelper.getAppStateClient();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多