2017-06-14 47 views
-1

我試圖根據玩家的數量生成一定數量的按鈕。但是,每當我嘗試將玩家人數從一個類別移動到另一個類別時,該應用就會崩潰。如何從Java中的第二個類訪問變量?

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

我試圖讓他們公共的和靜態的,我引用他們MainActivity.playerCount和MainActivity.players [I],但這只是導致應用程序崩潰。有什麼建議麼?

非常感謝您的幫助。如果您有任何問題,請告訴我。

回答

0

這是非常easy..Change MainActivity ...

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.. 
} 
} 
+0

我想你的解決方案,但它仍然崩潰時,我轉向第二項活動。有任何想法嗎? –

+0

它因爲類Player沒有實現android.io.Parcelable而崩潰。 –

+0

,我建議你只傳遞String數組,因爲你只是在MainActivity中收集玩家的名字。我會編輯我的答案。 –

相關問題