-1

i want set my textview become red color if the remain day is less than 3 day, but dnt knw why i get null pointed for the setText color code

ERROR LINE

I get remain.setTextColor(Color.RED); this line error

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

Error:

Caused by: java.lang.NullPointerException 
at my.com.chyi.schedulev2.assigmentActivity.onCreate(assigmentActivity.java:112)
Wei Chi
  • 29
  • 1
  • 8

2 Answers2

0

Make sure that your textView is mapped liked this

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

and please tell me on which line it gives error? Line number is 122 on your assigmentActivity.java but which line please code a line before this. Then I can help you..

Avishek Das
  • 661
  • 1
  • 6
  • 20
  • I am just using this. TextView tv; tv = (TextView)findViewById(R.id.techView); tv.setTextColor(Color.parseColor("#FF0000")); it is working for me. – Avishek Das Mar 23 '15 at 12:08
  • my code is exactly same with you, bt is stil get nullpointed speechless – Wei Chi Mar 23 '15 at 12:10
  • got it please cut the line remain = (TextView)findViewById(R.id.assigment_remain); and paste it after setContentView(R.layout.activity_assign); – Avishek Das Mar 23 '15 at 12:23
  • where is your onCreate function? I dont see it.. please make sure that the whole code from super.onCreate(savedInstanceState); to last within the onCreate() function. – Avishek Das Mar 23 '15 at 12:35
  • Like this: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ------------------------------------------ ------------------------------------------ } – Avishek Das Mar 23 '15 at 12:36
  • sry forget copy that line,posted at top – Wei Chi Mar 23 '15 at 12:38
  • sry jst come bck from dinner, i already add the header for the oncreate ez for you to see – Wei Chi Mar 23 '15 at 14:19
  • Your code will that format: public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //your code } } – Avishek Das Mar 23 '15 at 14:31
0

Try this and this is right way to give color to textView

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

Hope this will help you.

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35
  • 1
    Try this in your activity_assign.xml add this property in textView android:textColor="#FF0000" and remove this from java code - remain.setTextColor(getResources().getColor(R.color.Red)); – Sanwal Singh Mar 23 '15 at 13:26
  • i want change my textview in condition. – Wei Chi Mar 23 '15 at 14:31