0

Here is my code in a fragment:

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

    View view= inflater.inflate(R.layout.fragment_profile, container, false);
    profile();
    textView = (TextView) view.findViewById(R.id.textViewUsername);
    listView = (ListView) view.findViewById(R.id.listView);
    timer_start();
    return view;
}

public void timer_start(){
    final Runnable mTicker = new Runnable() {
        @Override
        public void run() {
            sendRequest();
            handler.postDelayed(mTicker, 5000); // error shows only for this line
        }
    };

    handler.postDelayed(mTicker, 5000);
}

I want to execute sendRequest() function in every 5 seconds. But it shows error: "Variable mTicker might not have been initialized" while I am calling timer_start() fragment.

Tanzila Islam
  • 93
  • 1
  • 12
  • Apologies for the sheer self-indulgence of dupe hammering the question with a question both asked and answered myself. I just think it exactly covers what you're asking here! – Andy Turner Jul 08 '17 at 16:05

1 Answers1

3

You are referring to mTicker in the same line that it is initialized. This is not allowed. That's like saying:

String s = s;

It doesn't make sense. Try using "this":

handler.postDelayed(this, 5000);
John Angland
  • 446
  • 3
  • 12
  • There is no error right now. Here, I am using navigation drawer. ProfileFragment is my default fargment. Now, default fragment shows correctly. But after adding this line, app stopped unfortunately while going the profilefragment from drawer menu. If I comment this line, everything goes alright. – Tanzila Islam Jul 08 '17 at 13:08
  • Interestingly though, you can write `String s = s = "";`. It's a quirk of the spec that I discovered this week. – Andy Turner Jul 08 '17 at 14:10