gpt4 book ai didi

java - 无法从回合制多人游戏的类中访问数据库

转载 作者:行者123 更新时间:2023-12-02 05:08:44 26 4
gpt4 key购买 nike

我正在使用谷歌的回合制多人游戏 API。在回合数据类中,我尝试访问我的 sqlite 数据库,以便将需要传递给下一个玩家的信息放入字节数组中。

这是转弯数据类:

public class BattleTurnData  extends Activity
{

public static final String TAG = "EBTurn";

public String gameData = "";
public int turnCounter;
public static Context passedContext;

public BattleTurnData(Context context)
{
passedContext = context;
}

// This is the byte array we will write out to the TBMP API.
public byte[] persist()
{
//SharedPreferences prefs = getSharedPreferences("Buildings", MODE_PRIVATE);
Database data = new Database(this);
data.open();
//int colonyHutOneID = prefs.getInt("NewColonyHutOne", 0);
int colonyHutOneLevel = data.getColonyHutOneLevel();
int colonyHutOneHealth = data.getColonyHutOneHealth();
int colonyHutTwoLevel = data.getColonyHutTwoLevel();
int colonyHutThreeLevel = data.getColonyHutThreeLevel();
int colonyHutFourLevel = data.getColonyHutFourLevel();
int colonyHutFiveLevel = data.getColonyHutFiveLevel();
data.close();

JSONObject retVal = new JSONObject();

try {
retVal.put("data", gameData);//colonyHutOneID + colonyHutOneLevel + colonyHutOneHealth + colonyHutTwoLevel);
retVal.put("turnCounter", turnCounter);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String st = retVal.toString();

Log.d(TAG, "==== PERSISTING\n" + st);

return st.getBytes(Charset.forName("UTF-16"));
}

// Creates a new instance of BattleTurn.
static public BattleTurnData unpersist(byte[] byteArray) {

if (byteArray == null) {
Log.d(TAG, "Empty array---possible bug.");
return new BattleTurnData(passedContext);
}

String st = null;
try {
st = new String(byteArray, "UTF-16");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return null;
}

Log.d(TAG, "====UNPERSIST \n" + st);

BattleTurnData retVal = new BattleTurnData(passedContext);

try {
JSONObject obj = new JSONObject(st);

if (obj.has("data")) {
retVal.gameData = obj.getString("data");
}
if (obj.has("turnCounter")) {
retVal.turnCounter = obj.getInt("turnCounter");
}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return retVal;
}
}//end BattleTurn

这是我的日志:

12-17 21:55:20.426: E/AndroidRuntime(1808): FATAL EXCEPTION: main
12-17 21:55:20.426: E/AndroidRuntime(1808): Process: com.project.llb, PID: 1808
12-17 21:55:20.426: E/AndroidRuntime(1808): java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
12-17 21:55:20.426: E/AndroidRuntime(1808): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:263)
12-17 21:55:20.426: E/AndroidRuntime(1808): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
12-17 21:55:20.426: E/AndroidRuntime(1808): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.twentytwentythree.sab.Database.open(Database.java:383)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.twentytwentythree.sab.BattleTurnData.persist(BattleTurnData.java:33)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.twentytwentythree.sab.BattleActivity.startMatch(BattleActivity.java:527)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.twentytwentythree.sab.BattleActivity.processResult(BattleActivity.java:667)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.twentytwentythree.sab.BattleActivity.access$0(BattleActivity.java:653)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.twentytwentythree.sab.BattleActivity$1.onResult(BattleActivity.java:232)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.twentytwentythree.sab.BattleActivity$1.onResult(BattleActivity.java:1)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.google.android.gms.common.api.a$c.b(Unknown Source)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.google.android.gms.common.api.a$c.handleMessage(Unknown Source)
12-17 21:55:20.426: E/AndroidRuntime(1808): at android.os.Handler.dispatchMessage(Handler.java:102)
12-17 21:55:20.426: E/AndroidRuntime(1808): at android.os.Looper.loop(Looper.java:157)
12-17 21:55:20.426: E/AndroidRuntime(1808): at android.app.ActivityThread.main(ActivityThread.java:5872)
12-17 21:55:20.426: E/AndroidRuntime(1808): at java.lang.reflect.Method.invoke(Native Method)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
12-17 21:55:20.426: E/AndroidRuntime(1808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)

顺便说一下,第 33 行是 data.open();

我不明白为什么我能够访问像这样的不同类中的数据库,但不能访问此类中的数据库。不过,我对这一切都很陌生,所以我还有很多东西要学。任何帮助表示赞赏。谢谢大家。

最佳答案

您已将 null 或无效的 Context 传递给 SQLiteOpenHelper 构造函数。

<小时/>

查看堆栈跟踪和代码,BattleTurnData 扩展了 Acitivity 和显式构造函数 BattleTurnData(Context context) 看起来很可疑。您无法使用 new 实例化 Activity ,并且框架实例化的 Activity 没有带参数的构造函数。因此,该类的 this 引用不是有效的 Context

看起来 BattleTurnData 不应该是一个 Activity ,而应该是一个常规类。将 Context 作为参数传递给需要它的方法。

关于java - 无法从回合制多人游戏的类中访问数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27539027/

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