动态设置textview的颜色
我通过以下代码使用自定义列表视图
public class details extends ListActivity {
/** Called when the activity is first created. */
Bundle extras;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
extras=getIntent().getExtras();
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"name","use"},
new int[] {R.id.text1,R.id.text2}
);
populateList();
setListAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList()
{
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("name","NAME");
temp1.put("use",extras.getString("name"));
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("name","CHANGE IN PRICE");
temp2.put("use",extras.getString("change"));
TextView txt=(TextView)findViewById(R.id.text2);
double k=Double.parseDouble(extras.getString("change"));
if(k<0)
{
txt.setTextColor(Color.RED);
}
else
{
txt.setTextColor(Color.RED);
}
list.add(temp2);
}
}
我想设置textview的颜色为绿色,如果价格改变大于0,否则我希望它是红色的。下面的代码在setTextColor()处抛出空指针异常。 我到底怎么做
链接地址: http://www.djcxy.com/p/87391.html