-1

I'm having some issues with using Butterknife and getting text from EditTexts. I have an activity with many EditTexts. For brevity sake, I'll only include 2-3 in my code example(s). I'm eventually taking the entered text and using it with iText's library to spit out a PDF.

First, I'm binding views with Butterknife:

public class NewPatientActivity extends AppCompatActivity {


@BindView(R.id.new_patient_editText_last_name)
TextInputEditText lastNameTextInputEditText;

@BindView(R.id.new_patient_editText_nick_name)
TextInputEditText nickNameTextInputEditText;

@BindView(R.id.new_patient_editText_date_of_birth)
TextInputEditText dateOfBirthTextInputEditText;

Then I ButterKnife.bind(this); in onCreate().

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_patient);
    ButterKnife.bind(this);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

Then, clicking on my floating action button is supposed to grab the text that was entered text in the EditTexts:

@OnClick(R.id.floating_action_button)
public void finishedPatientNotesFabClick(View view) {

    try {


        String lastNameText = lastNameTextInputEditText.getText().toString();

        String nickNameText = nickNameTextInputEditText.getText().toString();

        String dateOfBirth = dateOfBirthTextInputEditText.getText().toString();

    }

Then I try using those strings when I use them as the parameter for constructing a new Paragraph:

 private static void addBioAndContactInfoChapter(Document document) throws DocumentException {


    bioAndContactInfoChapter.add(new Paragraph(R.string.patient_last_name) + ": " +
            lastNameText);

    bioAndContactInfoChapter.add(new Paragraph(R.string.patient_nickname) + ": " +
            nickNameText);

    bioAndContactInfoChapter.add(new Paragraph(R.string.patient_date_of_birth) + ": " +
            dateOfBirth);

But here's where the problem is. When I use those strings, I get the

cannot resolve symbol '__string name'.

The Paragraph class has a constructor which only takes a String, so I don't think that's the issue. Is something funky happening because of Butterknife or what? Any help would be greatly appreciated!!!

enter image description here

Charuක
  • 12,953
  • 5
  • 50
  • 88
langsmith
  • 2,529
  • 1
  • 11
  • 13

1 Answers1

3

From the sniplets, your String lastNameText, nickNameText and dateOfBirth seem to be local to your method finishedPatientNotesFabClick. Most probably that's why they are not accessible inside addBioAndContactInfoChapter method. Declare those variables outside the method and assign the values to them inside the OnClickListener.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Still not working. I declared "private String lastNameText;" right after the initial Butterknife bind before onCreate(). Then in the onClickListener, I did " lastNameText = lastNameTextInputEditText.getText().toString();" but when I try to use the "lastNameText" string, [now I get "non-static field cannot be referenced from a static context"](http://imgur.com/a/kQiGD) – langsmith Dec 15 '16 at 19:08
  • Right. Your method is static. So your variable needs to be static as well, if you want to access it inside that method. Declare the variable as static. Or if you don't need the method to be static, remove that from the method. http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class might help in understanding more about static keyword. – Shobhit Puri Dec 15 '16 at 20:16