CounterClass无法解决

时间:2014-11-29 18:43:55

标签: android eclipse

这可能是一个非常简单的问题,但我似乎无法找到答案,所以任何帮助都会受到赞赏。

我试图运行Android应用程序,但代码中有错误。我在Internet上找到了代码,它应该可以正常工作但不是我的情况。在代码中有一行

final CounterClass timer = new CounterClass(180000,1000);

其中CounterClass带有下划线,并且有一条错误消息,指出CounterClass无法解析为某种类型。

我该怎么做才能摆脱这个错误?

package com.manish.timer;  
 import java.util.concurrent.TimeUnit;  
 import android.annotation.SuppressLint;  
 import android.annotation.TargetApi;  
 import android.app.Activity;  
 import android.os.Build;  
 import android.os.Bundle;  
 import android.os.CountDownTimer;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.TextView;  
 @TargetApi(Build.VERSION_CODES.GINGERBREAD)  
 @SuppressLint("NewApi")  
 public class MainActivity extends Activity {  
      Button btnStart, btnStop;  
      TextView textViewTime;  
      @Override  
        public void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.activity_main);  
          btnStart = (Button)findViewById(R.id.btnStart);  
          btnStop = (Button)findViewById(R.id.btnStop);  
          textViewTime = (TextView)findViewById(R.id.textViewTime);  
          textViewTime.setText("00:03:00");   
          final CounterClass timer = new CounterClass(180000,1000);  
          btnStart.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
              timer.start();  
            }  
          });  
          btnStop.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
              timer.cancel();  
            }  
          });  
        }  
      @TargetApi(Build.VERSION_CODES.GINGERBREAD)  
      @SuppressLint("NewApi")  
      public class CounterClass extends CountDownTimer {  
           public CounterClass(long millisInFuture, long countDownInterval) {  
                super(millisInFuture, countDownInterval);  
           }  
           @Override  
          public void onFinish() {  
            textViewTime.setText("Completed.");  
          }  
           @SuppressLint("NewApi")  
           @TargetApi(Build.VERSION_CODES.GINGERBREAD)  
           @Override  
           public void onTick(long millisUntilFinished) {  
                 long millis = millisUntilFinished;  
                  String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),  
                      TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),  
                      TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));  
                  System.out.println(hms);  
                  textViewTime.setText(hms);  
           }  
      }  
 }  

1 个答案:

答案 0 :(得分:0)

必须以特殊方式实例化内部类。语法是:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

所以在你的情况下,它应该是:

final MainActivity.CounterClass timer = this.new CounterClass(180000,1000);