简体   繁体   中英

EditText getText toString returns “”

I'm working on my first android application, but have some java experience (going to school for comp sci). I'm working on a 'high scores' system for a small game, and i can't get the name for a score from the EditText object.

public class SinglePlayerGame extends Activity {

    private Button mashButton, popButton;
    int taps;
    private TextView numberOutput, clock, popView;
    boolean first;

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

        taps = 0;
        first = true;

        numberOutput = (TextView) findViewById(R.id.editText1);
        mashButton = (Button) findViewById(R.id.mash_button1);
        clock = (TextView) findViewById(R.id.clockView);
        mashButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(first) {
                runClock();
                addOne();
                first = false;
                } else {
                    addOne();
                }
            }
        });
    }

    protected void runClock() {
         new CountDownTimer(30000, 1000) {

             public void onTick(long millisUntilFinished) {
                 clock.setText(DateUtils.formatElapsedTime(millisUntilFinished / 1000));
             }

             public void onFinish() {
                 mashButton.setEnabled(false);
                 clock.setText("Done!");
                 initPopWin();
             }
          }.start();
    }

    public void updateScores() {
        View layout = View.inflate(this, R.layout.popup_element, null);
        EditText nameInput = (EditText) layout.findViewById(R.id.popupName);
        String name = nameInput.getText().toString();
        MainMenu.scores.add(name + " : " + taps);
    }

    private PopupWindow pwindo;

    protected void initPopWin() {
        try { 
            // We need to get the instance of the LayoutInflater 
            LayoutInflater inflater = (LayoutInflater) SinglePlayerGame.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            View layout = inflater.inflate(R.layout.popup_element,(ViewGroup) findViewById(R.id.popup_element)); 

            popButton = (Button) layout.findViewById(R.id.btn_close_popup); 
            popButton.setOnClickListener(cancel_button_click_listener);

            popView = (TextView) layout.findViewById(R.id.txtView);
            popView.setText("Congrats your score was: " + taps + "!");

            pwindo = new PopupWindow(layout, 350, 350, true); 
            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

            } catch (Exception e) { 
                e.printStackTrace(); 
            }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() { 
        public void onClick(View v) { 
        updateScores();
        pwindo.dismiss();
        } 
    };


    protected void addOne() {
        // TODO Auto-generated method stub
        taps += 1;
        numberOutput.setText(Integer.toString(taps));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.single_player_game, menu);
        return true;
    }

}

The string is added to 'scores' which is a static arraylist in MainMenu, which works fine, but when shown scores say " : 100"

Any help would be appreciated.

Inflate the layout only once.

EditText nameInput; // Declare
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_player_game);

Then

protected void initPopWin() {
    try { 
        // We need to get the instance of the LayoutInflater 
        LayoutInflater inflater = (LayoutInflater) SinglePlayerGame.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View layout = inflater.inflate(R.layout.popup_element,null); 
        nameInput = (EditText) layout.findViewById(R.id.popupName);

Then

 public void updateScores() {
    String name = nameInput.getText().toString();
    MainMenu.scores.add(name + " : " + taps); 
}

I am not sure what MainMenu is.

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