Take the 2-minute tour ×

I have a listener that simply changes a button's text and clocks the time for the user. I also have a textview that is supposed to change to the new total amount of time worked when the user clocks out. I double checked the xml to make sure I was grabbing the right R.Id and the button in the listener is being found just not the TextView. Here is the code:

public class HoursListener implements OnClickListener{

GlobalApp appState;
Button startStopHoursButton;
TextView hoursTotalTextView;
public boolean clockedIn;

public HoursListener(Context ctx){
    appState = ((GlobalApp)ctx.getApplicationContext());

public void onClick(View v) {
    startStopHoursButton = (Button) v.findViewById(R.id.hourstogglebutton);
    hoursTotalTextView = (TextView) v.findViewById(R.id.totalWorktimeTextView);
        clockedIn = true;
        clockedIn = false;


Heres the xml for the textView:


The error is with this line:


I was thinking about just putting the code in the activity itself but I feel like I can do it this way. I want something I can call like a listener so I reduce the redundancy in my code. I have double checked that it is the hoursTotalTextView that is null. Yet the button isn't. Any ideas?

Screenshot of Eclipse (linked to full-size version) showing that the relevant values aren't null: Screenshot of Eclipse

share|improve this question
what is GlobalApp appState; –  Agarwal Shankar Apr 15 '12 at 16:14
its my application class I have for static variables. –  slai47 Apr 15 '12 at 17:20

4 Answers 4

up vote 1 down vote accepted

Presumably the onClickListener whose click you are listening to is the button? The onClick passes the View v that was clicked - i.e. the button - and the textbox is not a child of the button, so you can't find it.

If HoursListener is declared inside your activity, just do findViewById instead of v.findViewById i.e.

hoursTotalTextView = (TextView) findViewById(R.id.totalWorktimeTextView);

If not, pass the textview into the HoursListener constructor and set hoursTotalTextView there (bearing in mind this feels like it might cause memory leaks).

share|improve this answer
All I needed was to pass the TextView to the listener. Thank you so much. –  slai47 Apr 16 '12 at 4:48

what is GlobalApp appState; and why you are typecasting context to GlobalApp from below line. I think appstate will be null here.

appState = ((GlobalApp)ctx.getApplicationContext());

if GlobalApp is a class then create a object for it and then use getter and setter methods.

share|improve this answer
GlobalApp is my application I made so I can access certain static variables needed for my app to run. –  slai47 Apr 15 '12 at 17:02
can you paste that class because i am confused with this line appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().CreateFinishTimeStam‌​p(); –  Agarwal Shankar Apr 15 '12 at 17:05
as in extending class Application?u need to refine your code either GlobalApp or TextView may be null .... –  Its not blank Apr 15 '12 at 17:16
I'll explain it. App state has a current company that has a list of weeklyTimestamps. Those weeklytimestamps hold timestamps for that week and GetCurrentNewWeeklyTimestamps() either gets you this weeks time stamp or creates a new one for this week. CreateFinishTimestamp either starts a new timestamp or finishes one that is already created. –  slai47 Apr 15 '12 at 17:18
would be better if you paste you code –  Agarwal Shankar Apr 15 '12 at 17:19

you need to use

startStopHoursButton = (Button) v.findViewById(R.id.hourstogglebutton);
hoursTotalTextView = (TextView) v.findViewById(R.id.totalWorktimeTextView);

some where before,can be constructor cause your setText is working on null TextView object.

share|improve this answer
I have that whenever the onClick method is called. –  slai47 Apr 15 '12 at 17:03
try keepin it in constructor coz your textView is null or may be using wrong xml i guess... –  Its not blank Apr 15 '12 at 17:18
This should help : here –  slai47 Apr 15 '12 at 17:45

Check each object in that line

     Log.d("","getCurrentCompany is null");
     Log.d("","getCurrentNewWeeklyTimestamp is null");
     Log.d("","totalTimeDoneThisWeekToString is null");
share|improve this answer
all of those are not null. Debug shows that the TextView is null. –  slai47 Apr 15 '12 at 17:06
This should help : here –  slai47 Apr 15 '12 at 17:46
you should also note that your mainview is null too. Can you provide how MainActivity is creating HoursListener? And Is main.xml the only xml? Generally, TextView should be in Activity class. Please provide MainActivity.java too. –  Win Myo Htet Apr 15 '12 at 19:51

Your Answer


