我想知道如何动态改变TextView的颜色。看起来有一些东西在那里,但现在完全是我在找什么..我在这里做的是迭代一个数组,如果它的“+”我想要文本是格林如果它的“ - ”我想文本是红色的,问题是我得到空指针错误在ct.setTextColor(.....)任何人都有任何想法为什么?帮我改变TextView的颜色动态?
这里是XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tt"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bt"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
<TextView
android:id="@+id/pm"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:text="-"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
和代码片段
int c = count;
ind = (count);
j=0;
TextView ct = (TextView)findViewById(R.id.pm);
funkAdapter = (new ArrayAdapter<String>(this, R.layout.rowneg, R.id.pm));
String[] tmpAry= new String[count];
for (int i = 0; i < ind; i = i+2){
tmpAry[j] = dataAryArray[i];
if (tmpAry[j].equals("-")){
funkAdapter.add(tmpAry[j]);
ct.setTextColor(Color.RED);
}else{
funkAdapter.add(tmpAry[j]);
ct.setTextColor(Color.GREEN);
}
j++;
}
setListAdapter(funkAdapter);
添加堆栈跟踪你的异常PLZ, – Houcine
的这看起来可笑..可是我怎么获得这个? logcat的? – tricknology
您可以通过在您的Android SDK所在的工具文件夹中使用DDMS来获取堆栈跟踪。在插入手机的情况下启动DDMS,然后单击红色(E)符号以查找错误。 – Brian