无法解析方法TimeUnit行

时间:2016-11-20 07:01:53

标签: java android android-studio

以下是我的活动代码。

 @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())));
            }
        });
    }

显示错误无法解决我使用toMinutes的行的方法。和文字是红色。 我申请Alt+Enter但不起作用。 任何想法如何解决它?

3 个答案:

答案 0 :(得分:0)

您可能已导入此类:

java.util.concurrent.TimeUnit

将其替换为:

import java.util.concurrent.TimeUnit;

答案 1 :(得分:0)

即使我在eclipse中遇到同样的问题,我对TimeUnit的导入错误(即:import java.util.concurrent.TimeUnit无法恢复)。 在重新配置我的Build Path to Workspace默认JRE后,我能够修复它[这是我最新的]。 在Config Build Path之后 - 我解决了导入错误。附加快照BuildPathConfigration -

答案 2 :(得分:0)

也许最快的方法是使用此功能:

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);
}

您的代码将简化为

tx2.setText(msToString(finalTime));