0

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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sahil
  • 1
  • 3
  • You're calling `finish()` after setting the text in the current Activity... You won't be able to see that text very long.... If your app *crashed*, you need to add the logcat to your quesiton – OneCricketeer Sep 17 '17 at 21:08

1 Answers1

0
  1. Make sure all activities are in the manifest.

  2. Make sure all XML elements are in the correct layouts. For example, android:id="@+id/text" is not a part of activity_second.xml, so it is null.


In general, you are passing around Intent objects incorrectly. You have a cycle.

MainActivity starts the Second

private void confirm(String s){
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(intent);
}

Second then starts Main

Intent intent = new Intent(SecondActivity.this, 
MainActivity.class);
startActivity(intent);

And you could keep adding Activity instances to the stack this way.

What you need to do is see How to manage `startActivityForResult` on Android?

And Getting a Result from an Activity.


To start with, you can try this

public static final int CONFIRM_RESULT = 1;

private void confirm(String s){
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    intent.putExtra("passphrase", s);
    startActivityForResult(intent, CONFIRM_RESULT);
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245