0

我正在尝试将“玩家名称”添加到数据库中的表中。它可以很好地插入第一个,然后在第二个名称上崩溃。

public void addTeam1Members() { // TODO 自动生成的方法存根

    EditText playername, playerinit;
    DBAccessMatch dbAccess = new DBAccessMatch(this);

    String pname, pinit;
    dbAccess.open();
    for (int x = 0; x < team1Players; x++) {
        switch (x) {
        case 0:

            playername = (EditText) findViewById(R.id.etTeam1Player1);
            GlobalVars.setTeam1PlayerNames(playername.getText().toString(),
                    x);
            GlobalVars.sT1P1 = playername.getText().toString();
            playerinit = (EditText) findViewById(R.id.etTeam1Player1Init);
            GlobalVars.setTeam1PlayerInit(playerinit.getText().toString(),
                    x);
            if (playername.getText().toString().trim().isEmpty() == true) {
                pname = " ";
            } else {
                pname = playername.getText().toString().trim();
            }
            if (playerinit.getText().toString().trim().isEmpty() == true) {
                pinit = " ";
            } else {
                pinit = playerinit.getText().toString().trim();
            }
            Toast.makeText(getApplicationContext(), pname + " - " + pinit, Toast.LENGTH_SHORT).show();
            dbAccess.createPlayer(pname, pinit);
            break;
        case 1:
            playername = (EditText) findViewById(R.id.etTeam1Player2);
            GlobalVars.setTeam1PlayerNames(playername.getText().toString(),
                    x);
            GlobalVars.sT1P2 = playername.getText().toString();
            playerinit = (EditText) findViewById(R.id.etTeam1Player2Init);
            GlobalVars.setTeam1PlayerInit(playerinit.getText().toString(),
                    x);
            if (playername.getText().toString().trim().isEmpty() == true) {
                pname = " ";
            } else {
                pname = playername.getText().toString().trim();
            }
            if (playerinit.getText().toString().trim().isEmpty() == true) {
                pinit = " ";
            } else {
                pinit = playerinit.getText().toString().trim();
            }
            Toast.makeText(getApplicationContext(), pname + " - " + pinit, Toast.LENGTH_SHORT).show();
            dbAccess.createPlayer(pname, pinit);
            break;

这是显示玩家 1 和玩家 2 的一小部分代码。除了它们所在的编辑文本框之外,它们最终是相同的。

当 createPlayer(name,initial) 第一次运行时它会完美插入,然后第二次则不会。

    public void createPlayer(String playername, String playerinitials) {

    try {
        ContentValues cv = new ContentValues();
        cv.put(KEY_PLAYERNAME, playername);
        cv.put(KEY_PLAYERINITIALS, playerinitials);
        ourDatabase.insert(DATABASE_TABLE3, null, cv);

    } catch (Exception e) {
        String error = e.toString();
        Dialog d = new Dialog(null);
        d.setTitle("Dang it!");
        TextView tv = new TextView(null);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    }

}
4

1 回答 1

0

在对该问题进行一些研究后,我无法确定错误是什么(使用 logcat)。我将“创建用户”功能移到了别处,并在该课程之外进行了操作,一切正常。

于 2013-07-11T11:47:55.450 回答