1

So I have this project that I need to make as soon as I can and because of that I don't have much time to solve this myself.

I have two buttons: "Start" and "Stop", and I need my program to make a new date when I press "Start" and when I press "Stop" I need to make another date and to display both dates.

My problem is that, when I want to display both dates on press of the "Stop" button, the date that was created in the first button press does not exist.

I used setOnClickListener function for detecting the press of both my buttons.

public class MainActivity extends AppCompatActivity {

    private Button mStartButton;
    private Button mStopButton;

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


        mStartButton = (Button) findViewById(R. id. button_start);
        mStopButton = (Button) findViewById(R. id. button_stop);

        mStopButton.setClickable(false);

        mStartButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v ) {
                //start stoperica
                final Date datumpocetak = new Date();
                mStartButton.setClickable(false);
                mStopButton.setClickable(true);
            }
        });
        mStopButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v ) {
                //stop stoperica
                TextView display = (TextView)findViewById(R. id. display);
                Date datumkraj = new Date();

                String rezultat = new String();
                rezultat = ("od" + (datumpocetak) "do" + (datumkraj));
            }
        });
    }
}
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Aug 02 '18 at 21:42
  • Next time edit your post not put it at an answer because this can mix us. – Crammeur Aug 02 '18 at 22:41
  • Your manifest and activity_main look good. Try I `Clean Project` and `Rebuild Project` this can be a android studio bug. – Crammeur Aug 02 '18 at 22:55
  • And if that don't work try `Invalidate Caches / Restart...` – Crammeur Aug 02 '18 at 23:04
  • Thank you very much for your help, really appreciated! – Luka Bursac Aug 03 '18 at 12:34

1 Answers1

1

Is because your value datumpocetak is in onClick() method. You need to put it in the MainActivity.

Example [UPDATED]

public class MainActivity extends AppCompatActivity {

    private Button mStartButton;
    private Button mStopButton;
    Date datumpocetak = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        mStartButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v ) {
                //start stoperica
                datumpocetak = new Date();
                mStartButton.setClickable(false);
                mStopButton.setClickable(true);
            }
        });
        mStopButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v ) {
                //stop stoperica
                TextView display = (TextView)findViewById(R.id.display);
                Date datumkraj = new Date();

                String rezultat = new String();
                rezultat = ("od" + (datumpocetak) "do" + (datumkraj));
            }
        });
    }
}
Crammeur
  • 678
  • 7
  • 17