2015-02-25 4 views
0

Я начинаю разработку приложений для Android, у меня есть stoptimertask функция в моей mainactivity и кнопка stop в другой деятельности. То, что я хочу сделать, - это когда я нажимаю эту кнопку остановки из моей второй активности (которая является maps.class), я хочу, чтобы stoptimertask остановил, то есть остановил задачи. Однако приложение падает.Завершение таймера в android

Вот мой код mainactivity.java

public class MainActivity extends FragmentActivity{ 
    protected static final int CONTACT_PICKER_RESULT = 0; 
    int count=0; 
    Timer timer; 
    TimerTask timerTask; 
    final Handler handler = new Handler(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
sendBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      startTimer(); 
      sendSMSMessage(); 
      Intent toAnotherActivity = new Intent(MainActivity.this, maps.class); 
      startActivityForResult(toAnotherActivity, 0); 
    } 
     }); 
    } 
public void startTimer() { 

     timer = new Timer(); 
     initializeTimerTask(); 
     if(radioBtnten.isChecked()==true) 
     timer.schedule(timerTask, 5000, 10000); 
     // if(radioBtn2.isSelected()==true) 
     else if(radioBtnone.isChecked()==true) 
     timer.schedule(timerTask, 5000, 1000); 
    } 
    public void initializeTimerTask() { 

     timerTask = new TimerTask() { 
      public void run() { 
       handler.post(new Runnable() { 
        public void run() { 
         Toast.makeText(getApplicationContext(), "your message has been sent, the message(s) sent are:-"+count++,Toast.LENGTH_LONG).show(); 
         sendSMSMessage();  
        } 
       }); 
      } 
     }; 
    } 
    public void stoptimertask(View v) 
    { 
     //stop the timer, if it's not already null 

     Toast.makeText(getApplicationContext(), "Stop button pressed",Toast.LENGTH_LONG).show(); 

     if (timer != null) 
     { 
      timer.cancel(); 
      timer = null; 
      count = 0; 
     } 
     MainActivity.this.finish(); 
    } 
} 

здесь является maps.java (вторая деятельность)

public class maps extends FragmentActivity implements LocationListener { 

MainActivity call=new MainActivity(); 
    GoogleMap googleMap; 
    Button stop; 
    Timer timer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //show error dialog if GoolglePlayServices not available 
     if (!isGooglePlayServicesAvailable()) { 
      finish(); 
     } 
     setContentView(R.layout.maps); 
     stop = (Button)findViewById(R.id.stop); 


     stop.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View aView) 
         { 
           Intent toAnotherActivity = new Intent(aView.getContext(), MainActivity.class); 
           startActivityForResult(toAnotherActivity, 0); 
           Toast.makeText(getApplicationContext(), "Stop button pressed",Toast.LENGTH_LONG).show(); 
           maps.this.finish(); 
           call.stoptimertask(aView); 
         } 
     }); 

вот LogCat

FATAL EXCEPTION: main 
Process: com.example.textmessage, PID: 19869 
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105) 
    at com.example.textmessage.MainActivity.stoptimertask(MainActivity.java:167) 
    at com.example.textmessage.maps$1.onClick(maps.java:49) 
    at android.view.View.performClick(View.java:4756) 
    at android.view.View$PerformClick.run(View.java:19749) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5221) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
+0

Crash -> LogCat – 2Dee

+0

Возможно, нулевой указатель для таймера не был восстановлен? – eduyayo

ответ

1

Лучшее использование для такой тип сценария - Singleton.

MainActivity.java

public class MainActivity extends FragmentActivity 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     initializeTools(); 

     // Find reference of "sendBtn" with "findViewById" or other stuff 
     sendBtn.setOnClickListener(
       new View.OnClickListener() 
       { 
        @Override 
        public void onClick(View v) 
        { 
         startTimer(); 
        } 
       }); 
       // Rest of your code 
    } 

    private void initializeTools() 
    { 
     // Give context to Timers instance 
     Timers.getInstance().giveContext(this); 
    } 

    private void startTimer() 
    { 
     // Starts the timer when you click on "sendBtn" 
     Timers.getInstance().startTimers(); 
    } 
} 

Timers.java

public class Timers 
{ 
    private final ScheduledExecutorService scheduledExecutorService; 
    private final Runnable myTask; 
    private ScheduledFuture<?> futureTask; 

    private int count = 0; 

    private Context _context; 

    private static volatile Timers _timers; 

    private Timers() 
    { 
     super(); 

     // Your "futureTask manager" 
     scheduledExecutorService = Executors.newScheduledThreadPool(5); 

     // Good use is to instanciate task since it won't change on runtime 
     myTask = new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       // Your code to run after the delay has expired 
       Toast.makeText(_context, "your message has been sent, the message(s) sent are:-" + count++, Toast.LENGTH_LONG).show(); 

       // Same as the whole example, you should use the Singleton pattern to handle communications thanks to the Singleton class "Communicator" 
       Communicator.getInstance().sendSMSMessage(); 
      } 
     }; 
    } 

    // Allow only one instance of the class running. Anyone can get reference of the class with the static function Timers.getInstance(); 
    public static Timers getInstance() 
    { 
     if (Timers._timers == null) 
     { 
      synchronized (Timers.class) 
      { 
       if (Timers._timers == null) 
       { 
        Timers._timers = new Timers(); 
       } 
      } 
     } 

     return Timers._timers; 
    } 

    // For Toasts and other useful stuff 
    public void giveContext(Context context) 
    { 
     this._context = context; 
    } 

    // Stop the timer 
    public void stopTimer() 
    { 
     if (futureTask != null) 
     { 
      futureTask.cancel(true); 
     } 
    } 

    // Starts the task to happen in 10 seconds 
    public void startTimers() 
    { 
     futureTask = scheduledExecutorService.schedule(myTask, 10, TimeUnit.SECONDS); 
    } 
} 

А внутри любого класса приложения, используйте Timers.getInstance().stopTimer();, чтобы остановить таймер и Timers.getInstance().startTimer();, чтобы запустить его снова.

0

Вы попробовали?

mTimer = new Runnable() { 
    @Override 
    public void run() { 
     **return;** 
Смежные вопросы