无法从基于回合制的多人游戏的类访问数据库

时间:2014-12-18 04:00:04

标签: java android sqlite google-api applicationcontext

我正在使用google的回合制多人游戏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

这是我的logcat:

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();

的行

我不明白为什么我能够在这样的不同类中访问数据库,但不能在这个类中访问。我是所有这一切的新手,所以我还有很多东西需要学习。任何帮助表示赞赏。谢谢你们。

1 个答案:

答案 0 :(得分:2)

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


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

看起来BattleTurnData不应该是活动而是常规课程。传递Context作为需要它的方法的参数。