如何从Java中的第二个类访问变量?

时间:2017-06-14 05:04:53

标签: java android variables object static

我正在尝试根据玩家数量生成一定数量的按钮。但是,每当我尝试将玩家的数量从一个类移动到另一个类时,应用程序就会崩溃。

public class MainActivity extends AppCompatActivity {
    private EditText[] playersEditText = new EditText[10];

    public static Player[] players = new Player[10];
    public static int playerCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        playersEditText[0] = (EditText) findViewById(R.id.player0);
        playersEditText[1] = (EditText) findViewById(R.id.player1);
        playersEditText[2] = (EditText) findViewById(R.id.player2);
        playersEditText[3] = (EditText) findViewById(R.id.player3);
        playersEditText[4] = (EditText) findViewById(R.id.player4);
        playersEditText[5] = (EditText) findViewById(R.id.player5);
        playersEditText[6] = (EditText) findViewById(R.id.player6);
        playersEditText[7] = (EditText) findViewById(R.id.player7);
        playersEditText[8] = (EditText) findViewById(R.id.player8);
        playersEditText[9] = (EditText) findViewById(R.id.player9);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playerCount = 0;

                for (int i = 0; i < 10; i++) {
                    String name = playersEditText[i].getText().toString();

                    if (!(name.equals(""))) {
                        playerCount++;
                        players[playerCount] = new Player(name);
                    }
                }

                if (playerCount < 5)
                    Toast.makeText(MainActivity.this, "You need at least 5 players " + playerCount, Toast.LENGTH_SHORT).show();
                else {
                    startActivity(new Intent(MainActivity.this, PlayerRevealment.class));
                }
            }
        });
    }
}

以上是主要活动。我正在尝试访问以下类中的对象播放器和int playerCount。

public class PlayerRevealment extends AppCompatActivity {
    private int count = MainActivity.playerCount;
    private Button[] playerButtons = new Button[count];
    //private LinearLayout playerRevealmentLayout;
    //private LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_revealment);

        /*playerRevealmentLayout = (LinearLayout) findViewById(R.id.playerRevealmentLayout);

        for (int i = 0; i < playerButtons.length; i++) {
            playerButtons[i] = new Button(this);
            playerButtons[i].setText("Player " + i); //Change to actual names
            playerRevealmentLayout.addView(playerButtons[i], lp);
        }*/
        Toast.makeText(PlayerRevealment.this, MainActivity.players.length, Toast.LENGTH_SHORT).show();

我尝试将它们设为public和static,并使用MainActivity.playerCount和MainActivity.players [i]引用它们,但这只会导致应用程序崩溃。有什么建议吗?

提前感谢您的帮助。如果您有任何问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

很容易......改变主要活动......

   public class MainActivity extends AppCompatActivity {
private EditText[] playersEditText = new EditText[10];

// Use String array to store Player's names.
private String[] players=new String[10];
private int playerCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    playersEditText[0] = (EditText) findViewById(R.id.player0);
    playersEditText[1] = (EditText) findViewById(R.id.player1);
    playersEditText[2] = (EditText) findViewById(R.id.player2);
    playersEditText[3] = (EditText) findViewById(R.id.player3);
    playersEditText[4] = (EditText) findViewById(R.id.player4);
    playersEditText[5] = (EditText) findViewById(R.id.player5);
    playersEditText[6] = (EditText) findViewById(R.id.player6);
    playersEditText[7] = (EditText) findViewById(R.id.player7);
    playersEditText[8] = (EditText) findViewById(R.id.player8);
    playersEditText[9] = (EditText) findViewById(R.id.player9);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            playerCount = 0;

            for (int i = 0; i < 10; i++) {
                String name = playersEditText[i].getText().toString();

                if (!(name.equals(""))) {
                    playerCount++;
                    players[playerCount] = name;
                }
            }

            if (playerCount < 5)
                Toast.makeText(MainActivity.this, "You need at least 5 players " + playerCount, Toast.LENGTH_SHORT).show();
            else {
                Intent intent=new Intent(this,PlayerRevealment.class);
                intent.putExtra("yourCountKey",playerCount);
                intent.putExtra("yourArrayKey",players);
                startActivity(intent);
            }
        }
    });
}
 }

并在第二个活动中访问您的数据,如下所示..

public class PlayerRevealment extends AppCompatActivity {

//Use playerArray here.
private Player[] players = new Player[10];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_revealment);

    Intent intent = getIntent();
    int playerCount = intent.getIntExtra("yourCountKey", 5);
    String[] playerNames = intent.getStringArrayExtra("yourArrayKey");
    // Now create your players array.
    for (int i = 0; i < playerNames.length; i++) {
        players[i] = new Player(playerNames[i]);
    }
      //Do your stuff..
}
}