0

I get this error. not on any device, just on some. This is all the report:

java.lang.NullPointerException
at eu.innovaapps.tpark.ro.MainSms$4$1$1.run(MainSms.java:444)
at java.util.Timer$TimerImpl.run(Timer.java:284)

and it comes from:

buton.setEnabled(false);

                        Timer buttonTimer = new Timer();
                        buttonTimer.schedule(new TimerTask() {

                            @Override
                            public void run() {
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        buton.setEnabled(true);
                                    }
                                });
                            }
                        }, 5000);

When i press the button it gets disabled and after 5 seconds it gets enabled again. Line 444 is getActivity().runOnUiThread(new Runnable() {. I'd appreciate alot if you could help me solve this error.

EDIT: I got the same problem as here: Scheduled Task in Fragment returns getActivity as null

If the button in disabled, and i press the back button of the phone, i get this error which sends me at this part of the code. Is there a way i could stop the timer if the back button is pressed?

Community
  • 1
  • 1
victorholo
  • 105
  • 1
  • 11

2 Answers2

0

Can you please tell what is the line number 444 in your code ?

If line 444 is buton.setEnabled(true); then can you modify the code to add Button buton = (Button) findViewById(R.id. just before buton.setEnabled(true); and then try out the same code.

EDIT

Dear Victor,

I have read your EDIT... I used to get such exception... To go around this, what I have done is that I declared and static variable in my Activity class...

public static sActivity = null;

and then in onCreate method of Activity I assigned the current activity to it...

sActivity = this;

and then used it in the code instead of getActivity()...

So for e.g. my Activity name is myActivity then your code in that case could changed to...

 @Override
 public void run() {
      getActivity().runOnUiThread(new Runnable() 




  @Override
     public void run() {
          if (myActivity.sActivity != null) {
              myActivity.sActivity.runOnUiThread(new Runnable() 
          .
          .
          .
          }

Let me know if you find any other better solution...

-3
Timer notificationtask = new Timer();
notificationtask.schedule(new TimerTask() 
{
    @Override
    public void run() {
        // TODO Auto-generated method stub

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                            buton.setEnabled(true);

                    } catch (Exception e) {
                    }
                }
            });

    }
}, 1000, 1000);
codeMagic
  • 44,549
  • 13
  • 77
  • 93
Dakshesh Khatri
  • 639
  • 7
  • 12
  • Code-only answers are frowned upon on SO. Please explain *why* this answer should help and what you did. Also, please take the time to format your answers properly. – codeMagic Jan 27 '14 at 15:20