Setting color of a textview dynamically

I am using a custom list view through the following code

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);


    }





}

I would like to set the color of the textview to green if change in price is grater than 0 else i want it to be red in color .The following code throws a null pointer exception at the setTextColor(). How exactly do i dothis

链接地址: http://www.djcxy.com/p/87392.html

上一篇: 适用于Android的TextView Color

下一篇: 动态设置textview的颜色