简体   繁体   中英

EditText.getText().toString() in a fragment returns empty

I am trying to get an EditText.getText().toString() in a fragment working, I have already read all other questions on this issue but cannot find the solution: I have the EditText.getText().toString() in the onClick method for a confirm button after inflating the view, I have checked that the id of the button is correct, but I am not finding the error. In the logcat I can see that the method saveWord which is called after the line in question is called correctly - just the String that is supposed to be filled with the editText is always empty. If I put a hardcoded string on the variable, it works as expected.

Would be very glad if anyone could help! Might be just a silly mistake I am overlooking?

This is my code:

public class BingoAddNewFragment extends Fragment {

private static final String LOG_TAG = BingoDataManager.class.getSimpleName();

View v;

public static final String DEFAULT_EDIT_FRAGMENT_TAG = "editFragmentTag";


private Button mConfirmButton;
EditText mAddWords;

String newWord;

String bWord1 = ""; 
String bWord2 = ""; 
String bWord3 = ""; 
String bWord4= ""; 
String bWord5= ""; 
String bWord6= ""; 
String bWord7= ""; 
String bWord8= ""; 
String bWord9= ""; 
String bWord10= ""; 
String bWord11= ""; 
String bWord12= ""; 
String bWord13= ""; 
String bWord14= ""; 
String bWord15= ""; 
String bWord16= ""; 

String[] bWord = {bWord1, bWord2, bWord3, bWord4, bWord5, bWord6, bWord7, bWord8, bWord9,
        bWord10, bWord11, bWord12, bWord13, bWord14, bWord15, bWord16};


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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.add_bingo_fragment, container, false);

    //some other stuff

    mConfirmButton = v.findViewById(R.id.button_confirm);

    mAddWords = v.findViewById(R.id.editText_addWords);

    mConfirmButton.setOnClickListener (new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //The next line is not working (everything else seems fine) ->
            newWord = mAddWords.getText().toString();

            Log.d(LOG_TAG, "Begriff: " + newWord + " wird übergeben");
            saveWord (newWord);

        }
    });

    mSaveButton = v.findViewById(R.id.button_save_bingo);

    mSaveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveBingo();
        }
    });


public String[] saveWord (String newWord) {

    Log.d(LOG_TAG, "Wort: " + newWord + " entgegengenommen.");

    for (int i=0; i<16; i++) {

        Log.d(LOG_TAG,  "i steht bei: " +i);
        if (bWord[i].equals("")) {
            bWord[i] = newWord;
            Log.d(LOG_TAG, "Word als " + (i+1) + ". Begriff gespeichert.");
            break;
        }
    }
    return bWord;
}

And this is an extract of my layout file:

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorLightBlue"> <EditText android:id="@+id/editText_addWords" android:layout_width="206dp" android:layout_height="40dp" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:ems="10" android:hint="@string/input_word" android:inputType="textPersonName" android:textColor="@color/colorDarkBlue" android:textColorHint="@drawable/selector" android:textSize="12dp" app:layout_constraintEnd_toStartOf="@+id/button_confirm" app:layout_constraintHorizontal_bias="0.31" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText_title" /> 

I found a solution - though I am still not sure why it did not work. I changed the buttons into ImageButtons and suddenly it works perfectly fine.

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