简体   繁体   中英

Can't change TextView color

I need your help.

I want to change the color of TextViews in code, but get some difficulties, because nothing happens.

I've declared a TextView field in MainActivity and assigned my text object from layout to it. Then I call some method, that makes some calculation and writes the result into that textView object. When I catch an exception in this method, I'd like to change the color of my textView, but I don't get it. What should I look for to fix it?

Btw, when I try to change color in the onCreate method, it works well. Exceptions' catch block implements well too because I see that by other commands of that block. The Scale method works correctly if nothing throws any exceptions from it. Tried to find a similar problem, but guess I'm too far from the right way.

private TextView outputText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    outputText = findViewById(R.id.outputText);

    //this code works fine
    outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));

    scale();
}

public void scale() {
    try {

        //some command that can throw an exception

    } catch (Exception e) {
            
        //this command doesn't work
        outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
    }
}

UPD.1

here's code of the Scale method:

public void scale() {
    try {
        outMin = Double.parseDouble(String.valueOf(outMinField.getText()));
        outMinErr = false;
    } catch (Exception e) {
        outMaxErr = true;
        outMinField.requestFocus();
        outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
    }

    try {
        outMax = Double.parseDouble(String.valueOf(outMaxField.getText()));
        outMaxErr = false;
    } catch (Exception e) {
        outMaxErr = true;
        outMaxField.requestFocus();
        outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
    }

    //inMin & inMax gives another method
    double out = (outMax - outMin) / (inMax - inMin) * (in - inMin) + outMin;
    outputText.setText(String.format("%.2f", out));
    outputText.setTextColor(getResources().getColor(R.color.outputColor, getTheme()));
}

I know that I get an exception because I see a focused text input field I have trouble with.

I do not see any serious problems in your code, but I would suggest the following

public void scale() {
    boolean error = false;
    try {
        callThatMayThrowAnException();
    } catch (Exception e) {
        error = true;
    }
    Log.d("ViewColor", "Exception thrown: " + error);
    if(error)
       outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}

Now you can see in LogCat whether you caught an exception or your callThatMayThrowAnException() succeeded. If this will not help (error is caught but the view color has not changed) try to clean your project in Build menu and rebuild it.

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