已保存的游戏快照初始化(空对象引用) [英] Saved Games Snapshot initialization (null object reference)

查看:129
本文介绍了已保存的游戏快照初始化(空对象引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的已保存的游戏(快照)API

但我一直收到此错误:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.google.android.gms.games.snapshot.Snapshot.writeBytes(byte[])' on a null object reference


这是我正在尝试的代码


Here's the code for what I'm trying

Snapshot snapshot = new Snapshot();
snapshot.writeBytes(my_app_state);

// Save the snapshot.
SnapshotMetadataChange metadataChange
        = new SnapshotMetadataChange.Builder()
        .setDescription("Completed %" + (levelscompleted/totallevels) + " of levels.")
        .build();
Games.Snapshots.commitAndClose(getApiClient(), snapshot,
        metadataChange);


我试图弄清楚如何初始化快照,因为= new Snapshot(); 不起作用.

I'm trying to figure out how to initialize snapshot because = new Snapshot(); does not work.

推荐答案

Snapshot API在如何执行操作方面非常具体.以下是快照保存的高级过程:

The Snapshot API is very specific in how you must do things. The following is the high-level procedure for Snapshot save:

  • 打开快照
  • 解决冲突
  • 保存

以下代码来自

The following code is from the Google Play Games Snapshot sample which shows you how to use Snapshots cross-platform across Android and iOS.

首先,您必须打开快照并解决打开时的冲突.

First, you must open the snapshot and resolve conflicts on open.

/**
 * Prepares saving Snapshot to the user's synchronized storage, conditionally resolves errors,
 * and stores the Snapshot.
 */
void saveSnapshot() {
    AsyncTask<Void, Void, Snapshots.OpenSnapshotResult> task =
            new AsyncTask<Void, Void, Snapshots.OpenSnapshotResult>() {
                @Override
                protected Snapshots.OpenSnapshotResult doInBackground(Void... params) {
                    Snapshots.OpenSnapshotResult result = Games.Snapshots.open(getApiClient(),
                            currentSaveName, true).await();
                    return result;
                }

                @Override
                protected void onPostExecute(Snapshots.OpenSnapshotResult result) {
                    Snapshot toWrite = processSnapshotOpenResult(result, 0);

                    Log.i(TAG, writeSnapshot(toWrite));
                }
            };

    task.execute();
}

接下来,您必须解决冲突:

Next, you must handle conflict resolution:

/**
 * Conflict resolution for when Snapshots are opened.
 * @param result The open snapshot result to resolve on open.
 * @return The opened Snapshot on success; otherwise, returns null.
 */
Snapshot processSnapshotOpenResult(Snapshots.OpenSnapshotResult result, int retryCount){
    Snapshot mResolvedSnapshot = null;
    retryCount++;
    int status = result.getStatus().getStatusCode();

    Log.i(TAG, "Save Result status: " + status);

    if (status == GamesStatusCodes.STATUS_OK) {
        return result.getSnapshot();
    } else if (status == GamesStatusCodes.STATUS_SNAPSHOT_CONTENTS_UNAVAILABLE) {
        return result.getSnapshot();
    } else if (status == GamesStatusCodes.STATUS_SNAPSHOT_CONFLICT){
        Snapshot snapshot = result.getSnapshot();
        Snapshot conflictSnapshot = result.getConflictingSnapshot();

        // Resolve between conflicts by selecting the newest of the conflicting snapshots.
        mResolvedSnapshot = snapshot;

        if (snapshot.getMetadata().getLastModifiedTimestamp() <
                conflictSnapshot.getMetadata().getLastModifiedTimestamp()){
            mResolvedSnapshot = conflictSnapshot;
        }

        Snapshots.OpenSnapshotResult resolveResult = Games.Snapshots.resolveConflict(
                getApiClient(), result.getConflictId(), mResolvedSnapshot)
                .await();

        if (retryCount < MAX_SNAPSHOT_RESOLVE_RETRIES){
            return processSnapshotOpenResult(resolveResult, retryCount);
        }else{
            String message = "Could not resolve snapshot conflicts";
            Log.e(TAG, message);
            Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG);
        }

    }
    // Fail, return null.
    return null;
}

以下代码是在

这篇关于已保存的游戏快照初始化(空对象引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆