1

This may be a silly question, but I am struck halfway when I tried to create a simple app with a checkbox. When I use the checkbox to show and hide a single text, this works fine. But if it's for a second text, the app crashes. I wanted to hide one text and show another when the checkbox is clicked.

enter image description here

Here is the full code:

MainActivity.Java

package ***
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView txtHelloWorld;
    private TextView txtHelloWorldChecked;

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

        CheckBox checkBoxVisibility = findViewById(R.id.checkBox_visibility);
        txtHelloWorld = findViewById(R.id.txtHelloWorld);

        boolean isChecked = checkBoxVisibility.isChecked();

        updateTextVisibility(isChecked);

        checkBoxVisibility.setOnClickListener(v -> {

            boolean isChecked1 = ((CheckBox)v).isChecked();
            updateTextVisibility(isChecked1);
        });

    }

    private void updateTextVisibility(boolean isChecked) {
        if (isChecked) {
            txtHelloWorld.setVisibility(View.VISIBLE);
            txtHelloWorldChecked.setVisibility(View.INVISIBLE);

        } else {

            txtHelloWorld.setVisibility(View.INVISIBLE);
            txtHelloWorldChecked.setVisibility(View.VISIBLE);
        }
    }

}
speedflow
  • 75
  • 6
  • Can you share the error that you are receiving, – Emad Seliem Jul 18 '21 at 16:05
  • Private field 'txtHelloWorldChecked' is never assigned – speedflow Jul 18 '21 at 16:34
  • updated the original post with full code – speedflow Jul 18 '21 at 17:37
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Jul 18 '21 at 23:59
  • Also this really is _not_ the full code. – Ryan M Jul 18 '21 at 23:59
  • I am getting the error on line: private TextView txtHelloWorldChecked; and error message: Private field 'txtHelloWorldChecked' is never assigned – speedflow Jul 19 '21 at 00:37

1 Answers1

0

Your code is working fine with me without any problem ,you can try this solution as well

Replace this code

checkBoxVisibility.setOnClickListener(v -> {

   boolean isChecked1 = ((CheckBox)v).isChecked();
   updateTextVisibility(isChecked1);

});

by this code

 checkBoxVisibility.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateTextVisibility(isChecked);
        });
Emad Seliem
  • 608
  • 1
  • 4
  • 5