成员值字符串返回空值

时间:2019-03-09 20:49:28

标签: java android

我将值分配给了全局变量字符串,但未在create上访问

String depName //global variable

 teamText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), depName, Toast.LENGTH_SHORT).show();
            nm=depName;
        }
    });


    teamText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("value", teamText.getText().toString());
            editor.apply();
        }

        @Override
        public void afterTextChanged(Editable editable) {

            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            depName=sharedPreferences.getString("value","english");
        }
    });

 @Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    String text= adapterView.getItemAtPosition(i).toString();
    teamText.setText(text);





}

现在的问题是,当我单击文本视图时,该吐司随文本一起显示,但是当我想在create方法上对其进行访问时,它会为全局变量和editText.getText()提供空值。

0 个答案:

没有答案