简体   繁体   中英

Button that clears EditText(s) crashes my app

I'm relatively new with Java and Android programming, and I'm working on a simple Android app where you put something into EdiTexts, have one Spinner and two buttons. The first button (buttonspremi) shows the Toast message, and that works okay. The second button (buttonponisti) should clear all the EditTexts, but it always crashes my app. Can you please tell me what am I doing wrong? Thanks!

public class Glavni extends ActionBarActivity {

private Button buttonspremi;
private Button buttonponisti;


public EditText editTextIme, editTextPrezime, editTextAdresa, editTextOib, editTextTelefon;
private RadioButton radioButtonMusko, radioButtonZensko;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_glavni);
    Spinner s = (Spinner) findViewById(R.id.spinnerGradovi);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(
            this, R.array.gradovi, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);

    buttonspremi = (Button) findViewById(R.id.btnspremipodatke);

    buttonspremi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Toast.makeText(getApplicationContext(), "Podaci su spremljeni", Toast.LENGTH_LONG).show();

        }
    });

    buttonponisti = (Button) findViewById(R.id.btnponisti);
    buttonponisti.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editTextIme.setText(" ");
            editTextPrezime.setText(" ");
            editTextAdresa.setText(" ");
            editTextOib.setText(" ");
            editTextTelefon.setText(" ");
        }

    });

}

You have not initialized your references to the EditTexts you are trying to clear. You'll need to add lines like

editTextIme = (EditText) findViewById(R.id.editTextIme_id); editTextPrezime = ...

etc. for all the EditTexts inside your Activity's onCreate .

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