如果剩余日期少于3天,我想将我的textview设置为红色,但是dnt知道为什么我为null指向setText颜色代码
我得到了still.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);
}
@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)
答案 0 :(得分:0)
确保您的textView已映射为
TextView remain = (TextView)findViewById(R.id.textView1);
请告诉我它出错的哪一行?你的assigmentActivity.java上的行号是122,但是在这之前你需要编写一行代码。然后我可以帮助你..
答案 1 :(得分:0)
试试这个,这是为textView提供颜色的正确方法
textView.setTextColor(getResources().getColor(R.color.Red));
希望这会对你有所帮助。