-3

My MainActivity class has an if that checks is something is true it starts a new Activity which sets the text of a TextView to what I want.

MainActivity:

 public static int caseA = 1;

 if(caseA) {
     Intent i = new Intent(Timer.this, WorkTimerNotification.class);
     startActivity(i);
 }

then it goes to the new Activity and it should check the value of caseA.

WorkTimerNotification:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work_timer_notification);
    TextView timerMetric = (TextView)findViewById(R.id.tester_texter);
    if(MainActivity.caseA) {
            timerMetric.setText("min");
        }

Problem: It does not change the text. Also, lets say I have many other if statements in mainactivity like:

 if(caseB) {}
 if(caseC) {}
 //and so on..

How would I perform the checks? More importantly, how do I get this caseA check to work though.

Bryn
  • 257
  • 4
  • 16

1 Answers1

1

You need to call findViewById() in onCreate():

private TextView timerMetric;

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

    timerMetric = (TextView)findViewById(R.id.tester_texter);

    if(caseA) {
        timerMetric.setText("min");
    }
}

Right now you are calling it before calling setContentView(), before the view hierarchy is generated. For this reason, your code fails.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120