2

I want to make a button to clear the TextView text. Im using Android Studio.

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

    ImageButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            TextView.text="";
        }
    });
Cœur
  • 34,719
  • 24
  • 185
  • 251
Jan
  • 23
  • 3
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe stands with Ukraine Oct 15 '17 at 18:38
  • Possible duplicate of [Android - Set text to TextView](https://stackoverflow.com/questions/19452269/android-set-text-to-textview) – DiskJunky Oct 15 '17 at 18:45

3 Answers3

1

You have to instantiate the text view:

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

    TextView26 = (TextView) findViewById(R.id.<your text view id>);

    ImageButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            TextView26.setText=("");
        }
    });
Juan
  • 5,420
  • 2
  • 14
  • 26
0

You forgot to instantiate your views for ImageButton and TextView.

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

    ImageButton3 = (ImageButton) findViewById(R.id.myImgBtnId);
    TextView26 = (TextView) findViewById(R.id.myTvId);

    ImageButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            TextView26.text="";
        }
    });
HaroldSer
  • 1,952
  • 2
  • 11
  • 22
0

I would recommend you to name your variables in camel case.
Also if you can choose a more meaningful name it will be better.

You gotta find BOTH your views before you can manipulate them in your code.

ImageButton imageButton3; //name to clearTextButton?
TextView textView26; 

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

    imageButton3 = (ImageButton) findViewById(R.id.imgButtonId); //replace it with your true id in your xml
    textView26 = (TextView) findViewById(R.id.textViewId);

    imageButton3.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        textView26.setText("");
    }
});
Allen Wang
  • 31
  • 3