片段中的EditText.getText()。toString()返回空

时间:2019-02-19 16:01:44

标签: java android-edittext

我正在尝试在一个可用的片段中获取EditText.getText()。toString(),我已经阅读了有关此问题的所有其他问题,但找不到解决方案:我有EditText.getText()。toString( )在放大视图后,在确认按钮的onClick方法中,我检查了按钮的ID是否正确,但未找到错误。 在logcat中,我可以看到在有问题的行之后正确调用了方法saveWord-只有应该用editText填充的String始终为空。如果将硬编码的字符串放在变量上,它将按预期工作。

如果有人可以帮助,将非常高兴!可能只是我忽略的一个愚蠢的错误?

这是我的代码:

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;
}

这是我的布局文件的一部分:

<?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" />

1 个答案:

答案 0 :(得分:0)

我找到了解决方案-尽管我仍然不确定为什么它不起作用。 我将按钮更改为ImageButtons,然后突然工作正常。