开发者

Button to transfer me to next page and start a game

It's hard to explain in the title. Basically as far as I can tell the submit button is taking the name and placing it in the array like I want. What I need now is for the Play(done) Button to transfer the user and the data to the next class (which I believe is coded correctly) and I need it to start a game. The game which you will see in the second class (get the data from the previous) I need it to display the names from the names array 1 at a time and randomly assign them a task to do from the tasks array.

Currently the app is force closing after I click the play button. I'm linking both classes cause I'm pretty sure the problem is in the second class. Thanks for your help ahead of time.

Class1

public class Class1 extends Activity
{
    int players=0;
    String names[];

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.class1);

    final EditText input = (EditText) findViewById(R.id.nameinput);

    final ArrayList<String> names = new ArrayList<String>();



    Button submitButton = (Button) findViewById(R.id.submit_btn);
    submitButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View submit1)
        {
            players++;
            for(int i=0; i开发者_开发问答 < 6; i++)
            {
                names.add(input.getText().toString());

                input.setText("");
            }
        }
    });

    Button doneButton = (Button) findViewById(R.id.done_btn);
    doneButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View done1)
        {
            Intent done = new Intent(Class1.this, Game.class);
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("arrayKey", names);

            done.putExtra("players", players);
            //done.putExtra("names", names[players]);
            startActivity(done);
        }
    });
}

Game Class

public class Game extends Activity
{
    int players, counter=0, score, ptasks,rindex;
    String[] names, tasks;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

    Bundle bundle = this.getIntent().getExtras();
    String[] names = bundle.getStringArray("arrayKey");

    Intent game = getIntent();
    players = game.getIntExtra("players", 1);
    //names = game.getStringArrayExtra("names");

    Random generator = new Random();

    tasks[0]= "";
    tasks[1]= "";
    tasks[2]= "";
    tasks[3]= "";
    tasks[4]= "";
    tasks[5]= "";
    tasks[6]= "";
    tasks[7]= "";
    tasks[8]= "";
    tasks[9]= "";



    while (counter <5)
    {
        for (int i = 0; i < 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)
            {

            }
        });

        Button notButton = (Button) findViewById(R.id.notbad_btn);
        notButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View notbad)
            {

            }
        });

        Button champButton = (Button) findViewById(R.id.champ_btn);
        champButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View champp)
            {

            }
        });


        counter++;
    }


}

Thought I should also mention that these buttons on the 2nd page I would like to assign a score to whichever name array person is up and keep track until the final round where it will display the winner. If anyone has any idea how to make that work.


Have you made sure to include the Game activity as an Application Node in your AndroidManifest?

If not, open your manifest to the application tab, on the bottom hit Add, and add an Activity of the name .Game

Without this, that intent never gets received by the other class.


You've already been told that you use non-initialized arrays here: EditText and using buttons to submit them. But also you're trying to get array extra, however you don't put it inside intent. And you're using uninitialized tasks array in Game class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜