设置文本颜色为null



如果剩余天数小于3天,我想将我的文本视图设置为红色,但dnt knw为什么我的setText颜色代码为空

误差线

我得到保留。setTextColor(Color.RED);该线路错误

public class assigmentActivity extends Activity {
TextView remain;
protected void onCreate(Bundle savedInstanceState) {   
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_assign);
    dbcon = new SQLController(this);
    //assign module
    addass_bt = (Button)findViewById(R.id.addAss_bt_id);
    remain = (TextView)findViewById(R.id.assigment_remain);
    lv =(ListView)findViewById(R.id.assList_id);
    registerForContextMenu(lv);
   addass_bt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent add_ass = new Intent(assigmentActivity.this,Add_Assignment.class);
            startActivity(add_ass);
        }
    });
  dbcon.open();
   records = dbcon.getAllAssignment();
    dbcon.close();
List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();
    for (int i = 0; i<records.size(); i++) {
        HashMap<String, Object> hm = new HashMap<String, Object>();
        hm.put("txt", records.get(i).getAssname());
        hm.put("txt2", records.get(i).getAssTime());

        Calendar today = Calendar.getInstance();
        SimpleDateFormat dd = new SimpleDateFormat("dd/M/yyyy");
        String ez = records.get(i).getAssTime();
        //count the remain day
        Date t = new Date(ez);
        try {
            t= dd.parse(ez);
        } catch (Exception e1) {
        }
        //Date date1 = t.getTime();
        Date date2 = today.getTime();
        long diff = Math.abs(t.getTime() - date2.getTime());
        long diffDays = (diff / (24 * 60 * 60*1000)+1);
        //check the remain date
        int a = (int)diffDays;
         int b=3;
        if(a<=b){
          remain.setTextColor(Color.RED);
        }
      hm.put("txt3", String.valueOf(diffDays));
      aList.add(hm);
       }
    // Keys used in Hashmap
    String[] from = {"txt","txt2","txt3"};
    // Ids of views in listview_layout
    int[] to = { R.id.assigment_name,R.id.assigment_ATime,             R.id.assigment_remain}
   SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.view_assignment_entry, from, to);
    lv.setAdapter(adapter);

}

oncreate

   @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId()==R.id.assList_id) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.contextmenu_list, menu);
    }
}
  //delete_contextmenu
   @Override
   public boolean onContextItemSelected(MenuItem item) {
     AdapterView.AdapterContextMenuInfo info =   (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    long selectid = info.id; //_id from database in this case
    int selectpos = info.position;
    long id = Long.parseLong(records.get(selectpos).getAssID());
    //dbcon = new SQLController(this);
    switch(item.getItemId()) {
        case R.id.deletefromcontext:
            dbcon.open();
            dbcon.deleteDataA(id);
          //  Log.d("THJIWHJFMOICJMOIEC", String.valueOf(i));
            dbcon.close();
            Intent intent = new Intent(this, assigmentActivity.class);
            startActivity(intent);
            finish();
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

错误:

Caused by: java.lang.NullPointerException 
at my.com.chyi.schedulev2.assigmentActivity.onCreate(assigmentActivity.java:112)

确保您的textView像这个一样映射

TextView remain = (TextView)findViewById(R.id.textView1);

请告诉我它在哪一行出错?你的作业Activity.java的行号是122,但请在哪行之前编码一行。那我可以帮你。。

试试这个,这是给textView 赋予颜色的正确方法

textView.setTextColor(getResources().getColor(R.color.Red));

希望这对你有帮助。

最新更新