Mr.deva
Mr.deva

Reputation: 13

Cannot resolve method TimeUnit line

Below is my code for an Activity.

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

        b1 = (Button) findViewById(R.id.button);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        iv = (ImageView) findViewById(R.id.imageView);

        tx1 = (TextView) findViewById(R.id.textView2);
        tx2 = (TextView) findViewById(R.id.textView3);
        tx3 = (TextView) findViewById(R.id.textView4);
        tx3.setText("song.mp3");

        mediaPlayer = MediaPlayer.create(this, R.raw.song);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setClickable(false);
        b2.setEnabled(false);

        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Playing Now", Toast.LENGTH_LONG).show();
                mediaPlayer.start();

                finalTime = mediaPlayer.getDuration();
                startTime = mediaPlayer.getCurrentPosition();

                if (oneTimeOnly == 0){
                    seekBar.setMax((int) finalTime);
                    oneTimeOnly = 1;
                }
                   /* Problem below line */
                tx2.setText(String.format("%d min, %d sec",
                        TimeUnit.MILLISECOND.toMinutes((long) finalTime),
                        TimeUnit.MILLISECONDS.toMinutes((long) finalTime) -
                        TimeUnit.MINUTE.toSeconds(TimeUnit.MILLISECONDS.toMinutes())));
            }
        });
    }

It is showing the error cannot resolve method at line where I used toMinutes. and text is red color. I have apply Alt+Enter but does not work. any idea how to solved it?

Upvotes: 1

Views: 8224

Answers (3)

dkupchenko
dkupchenko

Reputation: 1

Maybe the fastest way is using this function:

String msToString(int ms) {

    int hh = ms / 3600000;
    ms -= hh * 3600000;
    int mm = ms / 60000;
    ms -= mm * 60000;
    int ss = ms / 1000;

    return String.format("%d min, %d sec", mm, ss);
}

Your code will be simplyfied to

tx2.setText(msToString(finalTime));

Upvotes: 0

Satyendra Sharma
Satyendra Sharma

Reputation: 1516

Even I faced same issue in eclipse, where my import for TimeUnit was erroring out (i.e.: import java.util.concurrent.TimeUnit can not be resoved). I was able to fix it after re configuring my Build Path to Workspace default JRE [which was latest in my case]. After Config Build Path- I got my import error resolved. Attaching snap BuildPathConfigration-

Upvotes: 0

Zeero0
Zeero0

Reputation: 2790

You might have imported this class:

java.util.concurrent.TimeUnit

Replace it with this one:

import java.util.concurrent.TimeUnit;

Upvotes: 0

Related Questions