开发者

TextView Android displays "null" under actual strings

Any ideas where the underlying word "null" may be coming from? This only appears on certain devices, most devices I've tested do not have this underlying word. Below is a partial screen shot showing the issue with the word "null" in my layout:

TextView Android displays "null" under actual strings

EDITED: Here is the xml:

        <TextView 
        android:id="@+id/day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:text="DAY: "
        style="@style/ForecastLayout.Detail.DayTextSizeBold"/>

and the code that populates it:

TextView dayTitle_tv = (TextView)v.findViewById(R.id.day);

buf = mContext.getApplicationContext().getResources().getString(R.string.day).toU开发者_如何学运维pperCase();

            dayTitle_tv.setText(buf + ":"); 

I do the same thing for the titles seen at the bottom of the screen image I uploaded:

// headings
            // precip
            tv = (TextView)v.findViewById(R.id.precip_title);
            tv.setText(mContext.getApplicationContext().getResources().getString(R.string.precip).toUpperCase());

            // day
            tv = (TextView)v.findViewById(R.id.day_title);
            tv.setText(mContext.getApplicationContext().getResources().getString(R.string.day).toUpperCase());

            // night
            tv = (TextView)v.findViewById(R.id.night_title);
            tv.setText(mContext.getApplicationContext().getResources().getString(R.string.night).toUpperCase());

and I do not get the "null" underlying. Hmmm?


I suspect because you're defining the text directly rather than referencing a resource file (as you should be doing), Android is seeing that there is no resource defined for this field.

It then says "oh, this field has nothing in it" and so assigns a null value to the Spannable that it uses to draw it, but also draws your text.

Try switching to using resource files and see if it helps.


if you want to access the android:text in you xml file use TextView.getText();

since you reference a string resource

R.string.day

it seems the there is no string called day if you want you can add it to your res/values/strings.xml

by adding this

<string
        name="day"
        >some value </string>

and add it to the text view like this

 <TextView 
    android:id="@+id/day"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:text="@string/day"
    style="@style/ForecastLayout.Detail.DayTextSizeBold"/>

so if you make this call

buf = mContext.getApplicationContext().getResources().getString(R.string.day).toUpperCase(); 

it won't return null value


I needed initialize my variable to something other than "null". I didn't see that another developer had initialized this variable to "null". I changed this to an empty string and the problem is solved!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜