简体   繁体   中英

Pass string to a service from activity

Alright, so i have an Activity. In the activity there is a textview and a button. The button launches a ColorPicker and when the color is chosen, it will place the hex value in the textview.

Now, in a service i am attempting to convert the string to a color int. Then set an imageview background color the hex value from the textview. See example below...

In my main.xml i have a textview and a button. The Textview will have the hex value set in its text.


In my service, i have an imageview. To set the background color of the imageview, i got the text from the textview in the main activity then made a string. Then i converted it to a Int. But when i dry to set the color as a background it will Force close!

`BatteryBarTop = (ImageView) view.findViewById(R.id.battery_bar_top);
String tbColor = Setting.ColorValue.getText().toString();
int color = Color.parseColor(tbColor);
BatteryBarTop.setBackgroundResource(color);`

If i put a hex value in for "color" it will work perfectly. But i need the hex value from the textview be the color as it can be changed when needed...

You are calling setBackgroundResource() . This expects a resource ID. Use setBackgroundColor() to set a color.

    BatteryBarTop = (ImageView) view.findViewById(R.id.battery_bar_top);
    String value = Setting.ColorValue.getText().toString();
    int setColor = Integer.parseInt(value);

    try {
        BatteryBarTop.setBackgroundColor(setColor);
    }
    catch (NumberFormatException e)
    {
        // handle the exception
    }

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