nexus490
nexus490

Reputation: 817

XML inflation and EditText

I've posted another question on this, but im asking another as the problem has been narrowed down some what. My problem is that I'm getting null pointer exception from inside the inner onClick at the line where the first String strTime is created. It has been suggested the reason for this is the poptest.xml not inflating properly. Can anyone see why this is happening?

I have this method:

@Override
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.newappt:

       final Dialog dialog = new Dialog(this);
       dialog.setContentView(R.layout.poptest);
       dialog.setTitle("Create New Appointment");
       dialog.setCancelable(true);

       Button buttoncancel = (Button) dialog.findViewById(R.id.Button01);
       buttoncancel.setOnClickListener(new OnClickListener() {

        // on click for cancel button
           @Override
           public void onClick(View v) {
               dialog.dismiss();
           }
       });

       Button buttonsave = (Button) dialog.findViewById(R.id.Button02);
       buttonsave.setOnClickListener(new OnClickListener() {

        // on click for save button   
           @Override
            public void onClick(View v) {

             String strTime = ((EditText) findViewById(R.id.evnttime)).getText().toString();

             String strTitle = ((EditText) findViewById(R.id.evnttitle)).getText().toString();

             String strDet = ((EditText) findViewById(R.id.evntdet)).getText().toString();



            cursor = getAppts();
            addAppt(strTime, strTitle, strDet);
            showAppts(cursor);
            dialog.dismiss();
    }
       });

       dialog.show();
        break;

    case R.id.delappt:
        rmvall();
        break;
    }

}

Upvotes: 1

Views: 114

Answers (3)

KarlKarlsom
KarlKarlsom

Reputation: 5868

my first guess would be that the findViewById inside the onClick listener is searching for R.id.evnttime as a child of buttonsave.

the findViewById method belongs to the View class and is therefore availlale to every sub class like Buttons, Layouts... It is searching up to the last children of the view you use it on.

Possible solutions here: find your views outside of the onClick method or use the findViewById function of the main view (dialog).

Upvotes: 0

Harsh Dev Chandel
Harsh Dev Chandel

Reputation: 763

         String strTime = ((EditText) dialog.findViewById(R.id.evnttime)).getText().toString();

         String strTitle = ((EditText) dialog.findViewById(R.id.evnttitle)).getText().toString();

         String strDet = ((EditText) dialog.findViewById(R.id.evntdet)).getText().toString();

Upvotes: 0

user
user

Reputation: 87064

Try this if your EditText are in the Dialog:

String strTime = ((EditText) dialog.findViewById(R.id.evnttime)).getText().toString();
String strTitle = ((EditText) dialog.findViewById(R.id.evnttitle)).getText().toString();
String strDet = ((EditText) dialog.findViewById(R.id.evntdet)).getText().toString();

Upvotes: 2

Related Questions