从回合制多人游戏的类访问数据库时出现问题



我正在使用谷歌的回合制多人游戏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, "==== PERSISTINGn" + 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 extends Acitivity和显式构造函数BattleTurnData(Context context)看起来很可疑。不能使用 new 实例化活动,并且框架实例化的活动没有构造函数获取参数。因此,该类的this引用不是有效的Context

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

相关内容

  • 没有找到相关文章

最新更新