我最近收到了一份崩溃报告,其中我可以看到在 CountDownTimer 的 onTick 方法中,getView() 调用返回 null。我使用 onTick 方法在 textView 中显示剩余时间。textView 位于片段内。
由于 CountDownTimer 在 UI 线程中运行,我不知道为什么会发生这种情况。
可能是什么原因导致此问题以及此问题的可能解决方法是什么?
谢谢!
我最近收到了一份崩溃报告,其中我可以看到在 CountDownTimer 的 onTick 方法中,getView() 调用返回 null。我使用 onTick 方法在 textView 中显示剩余时间。textView 位于片段内。
由于 CountDownTimer 在 UI 线程中运行,我不知道为什么会发生这种情况。
可能是什么原因导致此问题以及此问题的可能解决方法是什么?
谢谢!
我就是这样做的。在我自己的计数器类中扩展了 CountDownTimer,其中包含 TextView。
public class myCounter extends CountDownTimer
{
    TextView counter;
    public myCounter(final long millisInFuture, final long countDownInterval,
                    final TextView newCounter)
    {
        super(millisInFuture, countDownInterval);
        counter = newCounter;
        counter.setText("Left: " + millisInFuture);
    }
    @Override
    public void onFinish()
    {
        counter.setText("GO!");
    }
    @Override
    public void onTick(final long millisUntilFinished)
    {
        counter.setText("Left: " + millisUntilFinished / 1000);
    }
}
然后在我的活动中:
public class CountdownViewActivity extends Activity
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.countdownview);
            final Button startBtn = (Button) findViewById(R.id.startButton);
            //Send total time in milliseconds, the interval to display at, and the TextView from your xml it should display in
            final myCounter countdown = new myCounter(timeInSeconds * 1000, 1000, (TextView) findViewById(R.id.textView4));
            startBtn.setOnClickListener(startClick(countdown));
//MoreStuff
    }
    private OnClickListener startClick(final myCounter countdown)
    {
        return new OnClickListener()
        {
            @Override
            public void onClick(final View v)
            {
                countdown.start();
            }
        };
    }
}