juliano.net
juliano.net

Reputation: 8177

Context.getString causing "no such static field"

I'm trying to get a string from my strings.xml file inside an event handler, although I'm getting "No such static field".

Here is my code:

mSearchEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            Context context = view.getContext();

            if (hasFocus && mSearchEditText.getText().toString().trim() == context.getString(R.string.searchbar_address_label)) {
                mSearchEditText.setText("");
            }
            else if (!hasFocus && mSearchEditText.getText().toString().trim().isEmpty()) {
                mSearchEditText.setText(context.getString(R.string.searchbar_address_label));
            }
        }
    });

What is wrong with this code?

Thanks

UPDATE: This code was intended to show a hint inside the EditText. The R.string seems to be unavailable inside listener's functions. So, consider my question as "Is there a native way to show hints inside EditText instead of writing my own code?"

Upvotes: 3

Views: 5039

Answers (3)

Zon
Zon

Reputation: 19880

It is possible that you have identical ids among all project XML what is forbidden.

It can also be that you reference to an XML with identical filename and there is no such id.

Upvotes: 0

Shayan_Aryan
Shayan_Aryan

Reputation: 2042

In this situations, rebuilding project and uninstalling the app from device might help. It worked for me.

Upvotes: 2

skygeek
skygeek

Reputation: 1568

use context.getResources().getString(R.string.searchbar_address_label);

for setting hint use android:hint="your_text" in xml layout. or same can be done programmatically using edittext.sethint("text");

Upvotes: 2

Related Questions