nexus490
nexus490

Reputation: 817

Can't capture input to EditText fields

I have 3 EditText fields set up in a similar way to as follows:

<EditText
   android:id="@+id/evnttitle"
   android:layout_width="200dp"
   android:layout_height="wrap_content"
   android:background="#ffffff"
   android:inputType="textNoSuggestions"
   android:textColor="#000000"
   android:textCursorDrawable="@null" 
   android:layout_marginBottom="20dp">

   <requestFocus />
</EditText>

In my code they are set up like this outside of the onCreate:

EditText titlecap;

Then inside one of my methods i have this:

titlecap = (EditText) findViewById(R.id.evnttitle);

CharSequence titleconv = titlecap.getText();

addAppt(timeconv.toString(), titleconv.toString(), ...

This is one of many methods i've tried now as i seem to be having trouble getting strings from the edittext fields i keep getting null pointer exceptions, with this version its where i try to assign to the character sequences. How can i get what is typed into the EditTexts as a String?

LogCat:

enter image description here

LogCat as text:

FATAL EXCEPTION: main
java.lang.NullPointerException
at w1279057.CW2.CW2Organisor$2.onClick(CW2Organisor.java:88)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

Added OnCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(R.layout.main);

   View createappt = findViewById(R.id.newappt);
   createappt.setOnClickListener(this);
   View delappt = findViewById(R.id.delappt);
   delappt.setOnClickListener(this);

   calendar = (CalendarView) findViewById(R.id.cal);

   appts = new ApptsData(this);



}

Full on click 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) {

               timecap = (EditText) findViewById(R.id.evnttime);

               titlecap = (EditText) findViewById(R.id.evnttitle);

               detcap = (EditText) findViewById(R.id.evntdet);

               CharSequence timeconv = timecap.getText();

               CharSequence titleconv = titlecap.getText();

               CharSequence detconv = detcap.getText();


            cursor = getAppts();
            addAppt(timeconv.toString(), titleconv.toString(),       detconv.toString());
            showAppts(cursor);
            dialog.dismiss();
    }
       });

       dialog.show();
        break;

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

}

Upvotes: 0

Views: 856

Answers (1)

Shubhayu
Shubhayu

Reputation: 13552

Don't make the

View createappt and View delappt local to the onCreate()

Create them outside it so that it has access everywhere in the class.

View createappt = null;
View delappt = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    createappt = findViewById(R.id.newappt);
    createappt.setOnClickListener(this);

    delappt = findViewById(R.id.delappt);
    delappt.setOnClickListener(this);

    calendar = (CalendarView) findViewById(R.id.cal);

    appts = new ApptsData(this);
}

Adding the updates

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

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

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

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

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

Upvotes: 1

Related Questions