I have a chronometer in second activity. I wanted to access it from main activity so i put the chronometer in second activity as public static and "secondActivity.chronometer" in main activity but when launching the app it shows chronometer.setbase(long) on a null object reference
I've checked the xml id is correct. Does anyone know how to solve the problem?

- 74
- 2
- 10
-
1This is a terrible idea for several reasons, not least of all because you are almost certainly creating a memory leak by maintaining a static reference to a `View` (your `Chronometer`). Use an `Intent` or event bus or some other mechanism for communicating between your two Activities (see https://stackoverflow.com/questions/21393287/how-should-i-communicate-between-activities) – PPartisan Nov 05 '19 at 12:57
-
cause i'm actually wanted to let my chronometer running in background when launching main activity using ```Service``` does it also have to use ```Intent``` to access the chronometer? won't it affect the ```Service```? – Anonymous Nov 05 '19 at 13:11
-
By "in the background", I take it you mean it isn't visible? In that case, it doesn't need to be running - you just need to store the "start time" of your chronometer somewhere and then, when your chronometer *is* visible, call `Chronometer#setBase` using the time, and it will update to the correct difference between then and now. Have a look at my answer. – PPartisan Nov 05 '19 at 13:16
-
So I have to remove ```Intent``` as it just directly start the second activity instead of making it running in background which isn't visible? – Anonymous Nov 05 '19 at 13:41
-
Sure, you can invoke the `Intent` whenever you're ready to show the chronometer. Otherwise, you only need to store that "start time" somewhere. It could be in memory, or you can write it to `SharedPreferences` or something. – PPartisan Nov 05 '19 at 13:44
3 Answers
you have not initialized the chronometer. when you launch the app , chronometer don't have any value because it is in second activity .

- 818
- 10
- 25
When you launching the app, the second activity is not initialized. The chronometer , as a TextView's subclass, also cannot be initialized. It is not a good idea to access or change a view in other activity. You can transfer data and show by the data。

- 1
As I mentioned in my comment, retaining a static reference to a View
is a very bad idea. First, as other answers have made clear, there is not guarantee that your View
will have been initialised when you call it, as the Activity
it is part of may have not yet initialised. However, it also a prime cause of memory leaks (see this answer for more detail).
My recommendation would be that instead of starting your Chronometer
, instead make a note of the time you want it to start (which, in the case of Chronometer
, can be obtained by calling SystemClock#uptimeMillis
), writing it to an Intent
, and using that to launch your second Activity
. You can then use this time as the base time for your Chronometer
, and therefore give the impression that it has been running the entire time. I.e.
public class ActivityOne extends Activity {
//...
private void startChronometer() {
final Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("time", SystemClock.uptimeMillis());
startActivity(i);
}
}
public class ActivityTwo extends Activity {
private Chronometer chrono;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//...
chrono = findViewById(R.id.id_of_chronometer);
chrono.setBase(getIntent().getLongExtra("time", SystemClock.uptimeMillis()));
chrono.start();
}
}

- 8,173
- 4
- 29
- 48