Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I see two problems with your code that might explain why you get a <code>null</code> for your players list in your second <code>Activity</code>:</p> <ol> <li><p>In <code>Game</code>, <code>String[] names = bundle.getStringArray("arrayKey");</code> should be </p> <pre><code>ArrayList&lt;String&gt; names = bundle.getStringArrayList("arrayKey");` </code></pre></li> <li><p>In <code>Class1</code>, you're putting the <code>ArrayList</code> into the <code>Bundle</code>(<code>bundle.putStringArrayList("arrayKey", names);</code>) which is pointless since <code>bundle</code> goes no where. You should be putting it into the <code>Intent</code> instead:</p> <pre><code>done.putStringListExtra("arrayKey", names); </code></pre></li> </ol> <p>Note that your code is all the more confusing because you have both a <code>String []</code> named <code>names</code> and an <code>ArrayList</code> named <code>names</code> in different scopes. Decide on one (I'd recommend the <code>List</code>) and get rid of the other.</p> <p>Also, in <code>Game</code>, this is unncessary:</p> <pre><code>Bundle bundle = this.getIntent().getExtras(); String[] names = bundle.getStringArray("arrayKey"); Intent game = getIntent(); players = game.getIntExtra("players", 1); </code></pre> <p>You already have the <code>bundle</code> just before this, so you could as well do:</p> <pre><code>Bundle bundle = this.getIntent().getExtras(); String[] names = bundle.getStringArray("arrayKey"); players = bundle.getInt("players", 1); </code></pre> <p>The basic concept is that from the calling activity, you put information <em>into</em> an <code>Intent</code> using the various <code>putExtra()</code> and <code>putExtraXXX()</code> methods. In the <em>called</em> activity, you get the information you had put <em>into</em> the <code>Intent</code> by either</p> <ul> <li>getting a <code>Bundle</code> *from * the <code>Intent</code> via <code>getExtras()</code> and then getting everything put in using the various <code>get()</code> methods on the <code>Bundle</code> (<em>not</em> the <code>Intent</code>).</li> <li>directly invoking the <code>getExtraXXX()</code> methods on the <code>Intent</code>.</li> </ul> <hr> <p>For the second part, as your code currently stands, it simply going to loop over all the players immediately (5 times in all, I don't understand the purpose of <code>counter</code>).</p> <p>What you should instead be doing is performing all of your processing (calculating the score for the current player, incrementing the value of the player index, setting the next task etc) only when one of the 3 buttons is pressed. If it's going to be a long-lived task, you could disable the buttons until finished in order to enforce the requirement of allowing only one button to be pressed per player. Re-enable the buttons when the next player is ready.</p> <p>I don't have the energy to churn out everything you need but at a starting point, turn this:</p> <pre><code>public void onCreate(Bundle savedInstanceState) { //...other code here while (counter &lt;5) { for (int i = 0; i &lt; players; i++) { TextView name1 = (TextView) findViewById(R.id.pname); name1.setText( names[i]+":"); ptasks = 10; rindex = generator.nextInt(ptasks); TextView task = (TextView) findViewById(R.id.task); task.setText( tasks[rindex]); Button failButton = (Button) findViewById(R.id.fail_btn); failButton.setOnClickListener(new View.OnClickListener() { public void onClick(View failed) { return; } }); Button notButton = (Button) findViewById(R.id.notbad_btn); notButton.setOnClickListener(new View.OnClickListener() { public void onClick(View notbad) { return; } }); Button champButton = (Button) findViewById(R.id.champ_btn); champButton.setOnClickListener(new View.OnClickListener() { public void onClick(View champp) { return; } }); } counter++; } //...other code here } </code></pre> <p>into </p> <pre><code>public void onCreate(Bundle savedInstanceState) { //...other code here int i = 0; TextView name1 = (TextView) findViewById(R.id.pname); TextView task = (TextView) findViewById(R.id.task); Button failButton = (Button) findViewById(R.id.fail_btn); failButton.setOnClickListener(new View.OnClickListener() { public void onClick(View failed) { //do what must be done for the current player, calculate score, etc prepareNextPlayer(++i, names, name1, task); } }); Button notButton = (Button) findViewById(R.id.notbad_btn); notButton.setOnClickListener(new View.OnClickListener() { public void onClick(View notbad) { //do what must be done for the current player, calculate score, etc prepareNextPlayer(++i, names, name1, task); } }); Button champButton = (Button) findViewById(R.id.champ_btn); champButton.setOnClickListener(new View.OnClickListener() { public void onClick(View champp) { //do what must be done for the current player, calculate score, etc prepareNextPlayer(++i, names, name1, task); } }); //...other code here } private void prepareNextPlayer(int i, ArrayList&lt;String&gt; names, String [] tasks, TextView nameField, TextView taskField) { if(i &gt;= names.size()) { //all players have been processed, what happens now? return; } int rindex = generator.nextInt(10); nameField.setText( names.get(i)+":"); task.setText( tasks[rindex]); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload