简体   繁体   中英

get input from EditText field and save in a varibale

This is my first post,

I recently started dabbling in android studio just for fun really. I am trying to make an app for recording scores. I have all the basics set up, I have an EditText view to put the player name in. But I want the text "Danny is the winner" (or whatever name is in the EditText box of the winner) to display as a toast at the end. I have done this to currently say "player 1/2 is the winner". I know I just need to replace the player1/2 part with a variable name such as player one.

I have used,

// player 1 name

EditText playerOne = (EditText) findViewById(R.id.player1);
String player1 = playerOne.getText().toString();

//player 2 name

EditText playerTwo = findViewById(R.id.player2);
String player2 = playerTwo.getText().toString();

To get the info from the EditText view and then convert it to a String in the variable player1 and player2

I have then changed the text upon winning the game to say player1 + " is the winner", but when I run the app it crashes before opening. if I delete the 4 lines above it works fine....

Any help would be gratefully appreciated.

Thanks

My guess would be that you are having a nullPointerException. I think so because you might be asking the variables player1 and player2 to get the value in playerOne and playerTwo before they have any value. Basically what you need to do is get the string from playerOne and playerTwo only if it has something in it. Usually we put a button and on click of that button we execute the String player2 = playerTwo.getText().toString(); and String player1 = playerOne.getText().toString(); lines. Or something like waiting for an event to occur and then get the string from the EditTexts based on the Event. I rather you take an approach like that. Try this, I'm pretty sure this will work:

EditText playerOne = (EditText) findViewById(R.id.player1);
EditText playerTwo = findViewById(R.id.player2);
Button button = findViewById(R.id.button); //Make sure you define this is your xml file with the id button

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String player1 = playerOne.getText().toString();
            String player2 = playerTwo.getText().toString();
            //Set your toast here
        }
    });

Now make sure you define the button is your xml file with the id button , and that you click the button only after you enter something in both EditTexts . There are multiple ways to do the same thing so I suggest you look more into it if this is the case.

Go through this example

1-Create an edit text in your xml file

EditText editText = (EditText) findViewById(R.id.ed1);

2-You can save data in another variable like this

String strNumber = editText.getText().toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM