0

I am having an issue in setting up my Chronometer. The Java file shows an error at "updateTimerText" stating Cannot resolve method "updateTimerText (java.lang.string) How do I solve this? Everything is Fine on the fragment I am trying to cast this task to it is just the Chronometer.java file. If you need any other information or code, let me know. Thanks in advance.

package com.example.platinumirish.runassistwithdrawer;

import android.content.Context;

import runassist.demo.fragments.MainFragment;

/**
 * Created by Platinum Irish on 22/06/2016.
 */
public class Chronometer implements Runnable {

    public static final long MILLIS_TO_MINUTES = 60000;
    public static final long MILLIS_TO_HOURS = 3600000;

    private Context mContext;
    private long mStartTime;

    private boolean mIsRunning;

    public Chronometer(Context context){
        mContext = context;
    }

    public void start() {
        mStartTime = System.currentTimeMillis();
        mIsRunning = true;

    }

    public void stop() {

        mIsRunning = false;

    }

    @Override
    public void run() {

        while(mIsRunning) {

            long since = System.currentTimeMillis() - mStartTime;

                int seconds = (int) (since / 1000 % 60);
                int minutes = (int)(((since / MILLIS_TO_MINUTES)) % 60);
                int hours = (int)((since / (MILLIS_TO_HOURS)) % 24);
                int millis = (int) since % 1000;

                MainFragment mainFragment = (MainFragment) mContext.updateTimerText(String.format(
                    "%02d;%02d;%02d;%03d", hours, minutes, seconds, millis

            ));
        }

    }
}

Here is my Main Fragment too if it will help in any way, shape or form.

package runassist.demo.fragments;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

import com.example.platinumirish.runassistwithdrawer.R;


public class MainFragment extends Fragment{

private TextView mTvTime;
private Button mBtnStart;
private Button mBtnStop;

private Context mContext;
private Chronometer mChronometer;
private Thread mThreadChrono;


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

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    mTvTime = (TextView) rootView.findViewById(R.id.chronometer);
    mBtnStart = (Button) rootView.findViewById(R.id.start_button);
    mBtnStop = (Button) rootView.findViewById(R.id.stop_button);

    mBtnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(mChronometer == null) {
                mChronometer = new Chronometer(mContext);
                mThreadChrono = new Thread((Runnable) mChronometer);
                mThreadChrono.start();
                mChronometer.start();
            }
        }
    });

    mBtnStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mChronometer !=null) {
                mChronometer.stop();
                mThreadChrono.interrupt();
                mThreadChrono = null;

                mChronometer = null;
            }

        }
    });

    return rootView;

}

public void updateTimerText(final String time) {
    runOnUiThread(new Runnable() {

        @Override
        public void run () {
            mTvTime.setText(time);
        }

    });
}

private void runOnUiThread(Runnable runnable) {
}

}

1 Answers1

0

((MainFragment) mContext).updateTimerText(...) this is the way you want to call it. Check the squares around mContext. Also, your method updateTimerText returns void so this line:

MainFragment mainFragment = (MainFragment) mContext.updateTimerText(String.format(
                "%02d;%02d;%02d;%03d", hours, minutes, seconds, millis))

will raise another error. It should simply be:

((MainFragment) mContext).updateTimerText(...)

I can't help it, I have to suggest to use a different approach, first of all it's a bad idea to keep a reference to the context in a Runnable class.

Second of all, it's bad to cast Context to either Activity or Fragment, it will reduce the code reusability and cause unexpected crashes if the dev is not aware of this casts.

danypata
  • 9,895
  • 1
  • 31
  • 44
  • I have changed it to `((MainFragment) mContext).updateTimerText(String.format(` now I get another error at `((MainFragment) mContext)` saying "Inconvertable types; cannot cast android.content.context to runassist.demo.fragments.MainFragment" @danypata – Arron Potts Jun 22 '16 at 16:09
  • @ArronPotts You have to change the way you are communicating with the timer, it's wrong, I just posted an answers to your current problem. Please google a little bit on how to use timers and how to send/receive messages. – danypata Jun 22 '16 at 17:44