开发者社区> 问答> 正文

android在Activity实例化abstract类出错? 400 报错

android在Activity实例化abstract类出错? 400 报错

import java.util.Timer; import java.util.TimerTask;

import android.os.Handler;

public abstract class CountDown extends TimerTask {

private Timer timer = new Timer(true);
// 倒计时总的时间
private int totalSec;
// 临时变量 用来计算剩余时间
private int temp;
// 线程运行的标志位
private boolean running;

private int untilFinished;
final Handler cwjHandler = new Handler();
final Runnable onTickResults = new Runnable() {
    public void run() {
    	onTick(untilFinished);
    }
};
final Runnable onFinishResults = new Runnable() {
    public void run() {
    	onFinish();
    }
};
public CountDown(int totalSec) {
	this.totalSec = totalSec;
	
}

public void start() {
	if (temp == 0) {
		// 延迟一秒执行,间隔为一秒
		timer.schedule(this, 1000, 1000);
	}
	temp = 0;
	running = true;
}

public void stop() {
	running = false;
}

public boolean isRun(){
	return running;
}
@Override
public void run() {
	if (running) {
		untilFinished = totalSec - temp;
		cwjHandler.post(onTickResults);
		if (totalSec == temp) {
			cwjHandler.post(onFinishResults);
		}
		temp++;
	}

}

/**
 * 每次间隔执行
 * 
 * @param untilFinished
 *            剩余时间 (单位:秒)
 */
abstract void onTick(int untilFinished);

/**
 * 倒数计时结束后执行
 */
abstract void onFinish();

}

一个计时器的类,我在Activity中调用,出错了。

public void AntiDownTime(int time, int mod) {
		cdt = new CountDown(time) {
			@Override
			void onTick(int untilFinished) {
				// TODO Auto-generated method stub
				// 每1000毫秒回调的方法
				flashHandlerTime(untilFinished);
			}

			@Override
			void onFinish() {
				// TODO Auto-generated method stub
				cdt.stop();
			}
		};
		cdt.start();
	}

错误内容如下:

Multiple markers at this line
 - This class must implement the inherited abstract method CountDown.onTick(int), but cannot override it since it is not visible
  from new CountDown(){}. Either make the type abstract or make the inherited method visible
 - This class must implement the inherited abstract method CountDown.onFinish(), but cannot override it since it is not visible
  from new CountDown(){}. Either make the type abstract or make the inherited method visible

 

This class must implement the inherited abstract method CountDown.onFinish(), but cannot override it since it is not visible from new CountDown(){}. Either make the type abstract or make the inherited method visible

 

展开
收起
爱吃鱼的程序员 2020-06-04 15:18:32 776 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    but cannot override it since it is not visible from new CountDown(){}:


    这里是说子类看不到父类的方法(也就是无法继承)


    1 子类和父类不在同一个包里面


    2 你的父类的方法肯定没有写访问权限,默认的是包访问权限,在其他包里面是


    访问不到的,给父类的方法添上public

    2020-06-04 16:19:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载