Reputation: 123
After set textview's color at its property, this prompt appears at logcat when run, what does it mean?
when restore the textview color to its default, the error disappear and all is running well.
what's matter? how shall I set color of the textview correctly? Thanks,
11-11 00:45:56.302: E/TextView(2828): Saved cursor position 2/2 out of range for (restored) text
I forget what I changed in the prog and a prompt in logcat shows like this,
enter code here
11-11 01:40:48.102: E/AndroidRuntime(2921): Caused by: android.content.res.Resources$NotFoundException: File #ff0000 from drawable resource ID #0x7f050018: .xml extension required
11-11 01:40:48.102: E/AndroidRuntime(2921): at android.content.res.Resources.loadColorStateList(Resources.java:2255)
11-11 01:40:48.102: E/AndroidRuntime(2921): at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
11-11 01:40:48.102: E/AndroidRuntime(2921): at android.widget.TextView.<init>(TextView.java:956)
11-11 01:40:48.102: E/AndroidRuntime(2921): at android.widget.TextView.<init>(TextView.java:614)
11-11 01:40:48.102: E/AndroidRuntime(2921): ... 27 more
11-11 01:40:53.693: I/Process(2921): Sending signal. PID: 2921 SIG: 9
In the layout, I set
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
...
android:textColor="@string/TColor" />
In the string.xml,
<string name="TColor">#ff0000</string>
Upvotes: 1
Views: 863
Reputation: 5411
You need to store #ff0000 as a color not a string.
Put the color in your colors.xml file in the res/values folder of your project.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="my_color>#ff0000</color>
</resources>
Then in your text view use:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
...
android:textColor="@color/my_color" />
<Additional>
Reading your last comment, I think I see where you are confused.
Yes you can put different resource types all together in one XML file. But they must still be declared as their correct types. For example this first XML below is fine:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="t_color">#ff0000</color>
<color name="t_color_alternate">#dd0011</color>
<string name="text_1">First Text</string>
<string name="text_2">Second Text</string>
<color name="another_color">#ee3311</color>
<dimen name="my_margin">16dp</dimen>
<string name="text_3">Third Text</string>
</resources>
This second XML however will cause you problems when you try to use the resources because the types are incorrect:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="t_color">16dp</color>
<color name="t_color_alternate">Fred</color>
<color name="another_color">Rectangle</color>
<dimen name="my_margin">Blue</dimen>
</resources>
<Second Additional>
When you declare <string name="TColor">#ff0000</string>
the compiler will create a String object and fill it with the characters "#ff0000" when you run your app.
In other words, it is like writing String TColor = "#ff0000"
in code.
Simillarly, when you declare <color name="TColor">#ff0000</color>
the compiler will create a Color object and fill it with the color #ff0000 when you run your app.
This is like writing Color TColor = 0xff0000
in code.
If you read the Reference Documentation for Strings you will see it represents a sequence of characters. A Color on the other hand represents a sequence of integers.
Finally if you read the Reference Documentation for TextView
you will see that the XML attribute android:textColor
is equivalent to the method setTextColor(int)
. So when you write <string name="TColor">#ff0000</string>
you are trying to put a String into setTextColor(int)
.
Upvotes: 1