Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 课堂活动之外的变量_Java_Android_Class_Variables_Scope - Fatal编程技术网

Java 课堂活动之外的变量

Java 课堂活动之外的变量,java,android,class,variables,scope,Java,Android,Class,Variables,Scope,我这样做对吗?我正试图实现一个简单的倒计时,只是为了让我的脚与地面 为了整洁起见,我的timer类没有嵌套在我的活动中,而是单独嵌套在它自己的java文件中。在这里: public class McatTimer extends CountDownTimer { private TextView timer_tv; public McatTimer(long millisInFuture, long countDownInterval, TextView textview)

我这样做对吗?我正试图实现一个简单的倒计时,只是为了让我的脚与地面

为了整洁起见,我的timer类没有嵌套在我的活动中,而是单独嵌套在它自己的java文件中。在这里:

public class McatTimer extends CountDownTimer {

    private TextView timer_tv;

    public McatTimer(long millisInFuture, long countDownInterval, TextView textview) {
        super(millisInFuture, countDownInterval);
        this.timer_tv = textview;
    }

    @Override   
    public void onFinish() {
        timer_tv.setText("DONE!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        timer_tv.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

}
这里是logcat:

10-03 08:40:36.007: E/AndroidRuntime(2582): FATAL EXCEPTION: main
10-03 08:40:36.007: E/AndroidRuntime(2582): java.lang.NullPointerException
10-03 08:40:36.007: E/AndroidRuntime(2582):     at com.mangodeveloper.mcathomie.McatTimer.onTick(McatTimer.java:22)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.os.Looper.loop(Looper.java:123)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at java.lang.reflect.Method.invoke(Method.java:507)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 08:40:36.007: E/AndroidRuntime(2582):     at dalvik.system.NativeStart.main(Native Method)
我的活动在适当的地方有:

private McatTimer mcatTimer;

....

mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
mcatTimer.start();

阅读这些日志有什么诀窍吗?很抱歉,我正在自学。

看起来您的TextView对象为空。在调用之前,请确保已初始化TextView

mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
叫这个,

timerTv=(TextView)findViewById(R.id.textview);

你是如何初始化timerTv的?你知道,你是绝对正确的。我甚至没有想到这一点,因为它是所有字段,但计时器实例化出现在textview之前。是的,总是发生……)