Search code examples
javaandroidandroid-fragmentshandlerrunnable

Runnable Handler not executing inside fragment. Could not start a runnable


I have a runnable inside a fragment. The runnable is meant to update the textviews as well as receive input using buttons. But the program does not enter even once inside the runnable.

Please help. What am I doing wrong. Thanks. The code is as follows. I have buttons and textviews inside the runnable.

public class TodayFragment extends Fragment {
//initialisations

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

//UI initialisations

Handler mHandler = new Handler();
Runnable mTicker = new Runnable() {
            @Override
            public void run() {

//user interface interactions and updates on screen

    mHandler.postDelayed(mTicker, 1000);


   mTicker.run();

 }

Solution

  • try this change

    mTicker = new Runnable() {
            public void run() {
    
             //user interface interactions and updates on screen
    
               mHandler.postDelayed(mTicker, 1000);
    
            }
        };
        mTicker.run();