gpt4 book ai didi

android - 保存的游戏快照初始化(空对象引用)

转载 作者:搜寻专家 更新时间:2023-11-01 07:58:07 26 4
gpt4 key购买 nike

我正在尝试使用新的 Saved Games (Snapshot) API

但我一直收到这个错误:

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


这是我正在尝试的代码

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(); 不起作用

最佳答案

Snapshot API 对您必须如何做事有非常明确的规定。以下是快照保存的高级过程:

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

以下代码来自Google Play Games Snapshot sample其中向您展示了如何在 Android 和 iOS 之间跨平台使用 Snapshots。

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

/**
* 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();
}

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

/**
* 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;
}

以下代码是如何在 the Google Play Games Snapshots sample app 中完成的:

/**
* Generates metadata, takes a screenshot, and performs the write operation for saving a
* snapshot.
*/
private String writeSnapshot(Snapshot snapshot){
// Set the data payload for the snapshot.
snapshot.writeBytes(mSaveGame.toBytes());

// Save the snapshot.
SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
.setCoverImage(getScreenShot())
.setDescription("Modified data at: " + Calendar.getInstance().getTime())
.build();
Games.Snapshots.commitAndClose(getApiClient(), snapshot, metadataChange);
return snapshot.toString();
}

关于android - 保存的游戏快照初始化(空对象引用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com