You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

3个Activity计时器空值错误咨询:恢复按钮触发异常

Troubleshooting the Null Error in Your Pause Timer Activity

Hey there! Let's figure out why your resume button is throwing a null error while the main menu button works perfectly. Null pointer exceptions here almost always boil down to missing data or uninitialized references—let's break down the most likely causes and fixes:

1. Missing Timer State Data When Launching the Pause Activity

Your resume button needs access to the timer's current state (like remaining time, whether it was running, etc.) to pick back up, but it sounds like this data isn't being passed correctly to the Pause Activity. The main menu button probably doesn't need this data, so it avoids the error.

Fix: Pass critical timer state via Intent when starting the Pause Activity, then safely retrieve it in the Pause Activity's onCreate:

In your Timer Activity (when launching Pause):

Intent pauseIntent = new Intent(this, PauseTimerActivity.class);
// Pass remaining time and timer running state
pauseIntent.putExtra("REMAINING_TIME", currentRemainingTime);
pauseIntent.putExtra("TIMER_WAS_RUNNING", isTimerActive);
startActivity(pauseIntent);

In your Pause Activity's onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pause);

    // Retrieve data with default values to avoid nulls
    long remainingTime = getIntent().getLongExtra("REMAINING_TIME", 0);
    boolean wasTimerRunning = getIntent().getBooleanExtra("TIMER_WAS_RUNNING", false);

    // Use these values to initialize your resume logic
    Button resumeBtn = findViewById(R.id.btn_resume);
    resumeBtn.setOnClickListener(v -> {
        // Resume the timer using the retrieved remainingTime
        resumeTimer(remainingTime);
        // Navigate back to the Timer Activity if needed
        finish();
    });
}

2. Lost Reference to the Timer Instance

If you're trying to directly reference the CountDownTimer instance from your Timer Activity in the Pause Activity, you'll hit a null error—Activity instances aren't meant to be cross-referenced like that (they can be destroyed/recreated by the system). The main menu button doesn't rely on this reference, so it works.

Fix: Use a ViewModel to manage your timer state across Activities. ViewModels are lifecycle-aware, so they retain state even when Activities are recreated:

Create a Timer ViewModel:

public class TimerViewModel extends ViewModel {
    private long remainingTime;
    private boolean isTimerRunning;

    // Getters and setters for state
    public long getRemainingTime() { return remainingTime; }
    public void setRemainingTime(long time) { this.remainingTime = time; }
    public boolean isTimerRunning() { return isTimerRunning; }
    public void setTimerRunning(boolean running) { this.isTimerRunning = running; }
}

Update Timer Activity to use the ViewModel:

TimerViewModel timerViewModel = new ViewModelProvider(this).get(TimerViewModel.class);

// When pausing the timer, save state to the ViewModel
timerViewModel.setRemainingTime(currentRemainingTime);
timerViewModel.setTimerRunning(true);
// Then launch Pause Activity

Use the ViewModel in Pause Activity:

TimerViewModel timerViewModel = new ViewModelProvider(this).get(TimerViewModel.class);

Button resumeBtn = findViewById(R.id.btn_resume);
resumeBtn.setOnClickListener(v -> {
    long resumeTime = timerViewModel.getRemainingTime();
    boolean wasRunning = timerViewModel.isTimerRunning();
    
    // Resume your timer with this state
    if (wasRunning) {
        startTimer(resumeTime);
    }
    finish();
});

3. Uninitialized Resume Button (Less Likely)

Double-check that your resume button is properly bound in the Pause Activity. If findViewById returns null (e.g., wrong ID, layout not inflated), clicking it will throw a null error. The main menu button works, so this is less probable, but worth verifying:

Button resumeBtn = findViewById(R.id.btn_resume);
if (resumeBtn == null) {
    Log.e("PauseActivity", "Resume button not found! Check layout ID.");
} else {
    resumeBtn.setOnClickListener(v -> { /* resume logic */ });
}

Start with checking the data passing first—it's the most common culprit here. Once you ensure the Pause Activity has all the state it needs, the resume button should work without null errors.

内容的提问来源于stack exchange,提问作者Manos Rasidakis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:30:19