0

I've been working on this project for the past few days and now I encountered an error. I've searched on google and stackoverflow for solutions but they didn't help (or I do not know how to fix it).

I'm trying to save the data from the chronometer from TimerActivity after I press the button "stop" and make it appear on HistoryActivity.

TimerActivity

public class TimerActivity extends AppCompatActivity {
    Toolbar toolbar;

    private ImageButton imgbtnPause;
    private ImageButton imgbtnReset;
    private ImageButton imgbtnStart;
    private ImageButton imgbtnStop;
    private Chronometer mChronometer;


    long saveTime;
    private long lastPause;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        imgbtnPause = (ImageButton) findViewById(R.id.imgButtonPause);
        imgbtnReset = (ImageButton) findViewById(R.id.imgButtonReset);
        imgbtnStart = (ImageButton) findViewById(R.id.imgButtonStart);
        imgbtnStop = (ImageButton) findViewById(R.id.imgButtonStop);

        mChronometer = (Chronometer) findViewById(R.id.chronometer);
mChronometer.setText("00:00:00");
        mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

            @Override
            public void onChronometerTick(Chronometer chronometer) {
                CharSequence text = chronometer.getText();
                if (text.length() == 5) {
                    chronometer.setText("00:" + text);
                } else if (text.length() == 7) {
                    chronometer.setText("0" + text);
                }
            }
        });

        imgbtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastPause != 0) {
                    mChronometer.setBase(mChronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
                } else {
                    mChronometer.setBase(SystemClock.elapsedRealtime());
                }
                mChronometer.start();
                imgbtnStart.setEnabled(false);
                imgbtnPause.setEnabled(true);
            }
        });

        imgbtnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastPause = SystemClock.elapsedRealtime();
                mChronometer.stop();
                imgbtnPause.setEnabled(false);
                imgbtnStart.setEnabled(true);
            }
        });

        imgbtnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChronometer.stop();
                mChronometer.setBase(SystemClock.elapsedRealtime());
                lastPause = 0;
                imgbtnStart.setEnabled(true);
                imgbtnPause.setEnabled(false);
            }
        });

    imgbtnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChronometer.stop();
                final AlertDialog.Builder builder = new AlertDialog.Builder(TimerActivity.this);
                builder.setTitle("Stop alert");
                builder.setMessage("Have you finished working mate?");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mChronometer.stop();
                        mChronometer.setBase(SystemClock.elapsedRealtime());
                        imgbtnStart.setEnabled(true);
                        final AlertDialog.Builder builder1 = new AlertDialog.Builder(TimerActivity.this);
                        builder1.setTitle("Save");
                        builder1.setMessage("Do you want to save your progress mate?");
                        builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                saveTime = SystemClock.elapsedRealtime() - mChronometer.getBase();
                                String seconds = String.valueOf(saveTime);
                                Intent i = new Intent(getApplicationContext(), HistoryActivity.class);
                                i.putExtra("Timer", seconds);
                                startActivity(i);

                                mChronometer.stop();
                                mChronometer.setBase(SystemClock.elapsedRealtime());
                                imgbtnStart.setEnabled(true);
                            }
                        });
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.timer_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.id_language) {
            Toast.makeText(this, "Language is clicked", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.id_logout) {
            Intent intent = new Intent(TimerActivity.this, MainActivity.class);
            startActivity(intent);
        } else if (id == R.id.id_history) {
            Intent intent2 = new Intent(TimerActivity.this, HistoryActivity.class);
            startActivity(intent2);
        }
        return super.onOptionsItemSelected(item);
    }
}

TimerActivity xml

<Chronometer
        android:id="@+id/chronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="192dp"
        android:textColor="#e78200"
        android:textSize="32sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

and plus four Image Buttons (start, stop, pause and reset).

HistoryActivity

public class HistoryActivity extends AppCompatActivity{

        TextView textView;
        Toolbar toolbar_h;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_history);

            toolbar_h = (Toolbar) findViewById(R.id.toolbar_history);
            setSupportActionBar(toolbar_h);

            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                String value = extras.getString("Timer");
                //The key argument here must match that used in the other activity
            }
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.history_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.id_language) {
            Toast.makeText(this, "Language is clicked", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.id_logout) {
            Intent intent = new Intent(HistoryActivity.this, MainActivity.class);
            startActivity(intent);
        }else if (id == R.id.id_timer){
            Intent intent2 = new Intent(HistoryActivity.this, TimerActivity.class);
            startActivity(intent2);
        }
        return super.onOptionsItemSelected(item);
    }
}

HistoyActivity XML has only one TextView.

How can I make the saved chronometer data appear on HistoryActivity?

Thank you

Rino
  • 1,215
  • 1
  • 16
  • 28
Anevo
  • 195
  • 1
  • 1
  • 10
  • You could call `setText()` on that `TextView`, supplying `value` as the text to display. – CommonsWare Jan 31 '18 at 23:11
  • Before I tried the coded mentioned above, I had this: In **TimerActivity** -> `long seconds = (int)(saveTime);` and in **HistoryActivity** instead of having Bundle extras = .... I had this -> `int value = getIntent().getExtras().getInt("Timer"); TextView textView = new TextView(this); textView.setTextSize(30); textView.setText(value); setContentView(value);` **HistoryActivity** gave me something* but not what I need. – Anevo Jan 31 '18 at 23:23
  • The only sort of `int` that you can pass to `setText()` is a string resource ID (e.g., `R.string.app_name`). You need to pass a string representation of your `int` to `setText()`. – CommonsWare Jan 31 '18 at 23:32
  • @CommonsWare I changed `textView.setText(value)` to `textView.setText(" "+value)` then to `textView.setText(String.valueof(value))` and got this error both times: `android.content.res.Resources$NotFoundException: Resource ID #0x0` – Anevo Jan 31 '18 at 23:40
  • I suspect that you are getting that error on some other line of your code. – CommonsWare Jan 31 '18 at 23:42
  • I'm going to add the rest of the code from **TimerActivity**, if you want, take a look at it. Thank you. – Anevo Jan 31 '18 at 23:45
  • If an error rises in `TimerActivity` then check the `ids` in `onOptionsItemSelected` are they correct – Yupi Feb 01 '18 at 00:10
  • @Yupi The error rises in **HistoryActivity** -> `java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.economic_timer/com.example.economic_timer.HistoryActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0` – Anevo Feb 01 '18 at 00:17
  • Then check the `ids` in `onOptionsItemSelected` in `history_menu` xml are they correct and also check in layout `activity_history` just to make sure are you have all ids which are you using in `Activity` – Yupi Feb 01 '18 at 00:28
  • Yes, they are corect. I get the error in **HistoryActivity** at `setContentView(value)`. – Anevo Feb 01 '18 at 00:42

0 Answers0