I'm trying to update my TextView from another class, but it keeps crashing.
It originally says "The app is locked", and takes the user to the next layout where they enter the passcode.
When the user enters the correct password, the app takes the user to the previous with the textview changed saying "the app is now unlocked!"
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button)findViewById(R.id.btn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirm(login.toString());
}
});
}
private void confirm(String s){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
Second Activity:
public class SecondActivity extends AppCompatActivity implements
View.OnClickListener {
public boolean first = false;
public boolean second = false;
public boolean third = false;
public boolean fourth = false;
public boolean submit = false;
public TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button first = (Button) findViewById(R.id.btn1);
first.setOnClickListener(this);
Button second = (Button) findViewById(R.id.btn2);
second.setOnClickListener(this);
Button third = (Button) findViewById(R.id.btn3);
third.setOnClickListener(this);
Button fourth = (Button) findViewById(R.id.btn4);
fourth.setOnClickListener(this);
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(this);
textView = (TextView)findViewById(R.id.text);
}
public int counter;
public void onClick(View v) {
counter++;
switch (v.getId()) {
case R.id.btn1:
if (counter == 1) {
first = true;
}
break;
case R.id.btn2:
if (counter == 2) {
second = true;
}
break;
case R.id.btn3:
if (counter == 3) {
third = true;
}
break;
case R.id.btn4:
if (counter == 4) {
fourth = true;
}
break;
case R.id.submit:
submit = true;
open();
break;
}
}
public void open() {
if (first && second && third && fourth && submit) {
textView.setText("Your App is now Unlocked!");
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
}
MainActivity layout:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="unlock"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Welcome to the app! The app is LOCKED!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
</android.support.constraint.ConstraintLayout>