使用getArguments()将int从活动传递到片段始终返回0

时间:2016-07-22 17:43:02

标签: java android android-fragments

我看过无数其他帖子看到尝试让这个工作没有运气,不幸的是,所以这里。

我将4个变量从一个活动传递给一个对话框片段。 3个int和一个字符串。 String值成功,但int总是设置为零。该字符串是从另一个实现Parcelable的类接收的。

static final String P1_ROUDN_WINS = "user1RWins";
static final String P2_ROUDN_WINS = "user2RWins";
static final String NUM_ROUNDS = "numberOfRounds";
static final String PLAYER_NAME = "winnersName";

活动

bundle = new Bundle();
bundle.putInt(P1_ROUDN_WINS, p1RoundWins);
bundle.putInt(P2_ROUDN_WINS, p2RoundWins);
bundle.putInt(NUM_ROUNDS, numLegs);
bundle.putString(PLAYER_NAME, newPlayers.get(currentPlayer -1).getPlayerName());
FragmentManager fm = getFragmentManager();
GamerOverDialog dialogFragment = new GamerOverDialog ();
                dialogFragment.setArguments(bundle);
                dialogFragment.show(fm, "Game Over");

片段

public void onViewCreated(View view, Bundle savedInstanceState) {
    rWins = (TextView) rootView.findViewById(R.id.numWinTV);
    rLose = (TextView) rootView.findViewById(R.id.numLoseTV);
    rounds = (TextView) rootView.findViewById(R.id.numRoundsTV);
    name = (TextView) rootView.findViewById(R.id.playerNameTV);
    Log.i("Game over dialog: " , getArguments().getInt(Scoreboard.P1_ROUDN_WINS)+" wins");
    String roundWins = ""+ getArguments().getInt(Scoreboard.P1_ROUDN_WINS);
    String roundLose = ""+ getArguments().getInt(Scoreboard.P2_ROUDN_WINS);
    String numLegs = ""+ getArguments().getInt(Scoreboard.NUM_ROUNDS);
    String winnersName = "" + getArguments().getString(Scoreboard.PLAYER_NAME);
    rWins.setText(roundWins);
    rLose.setText(roundLose);
    rounds.setText(numLegs);
    name.setText(winnersName);
}

我知道还有其他类似的帖子,但我尝试的都没有。任何帮助理解我做错了什么都会很棒。 感谢

0 个答案:

没有答案