I have made a custom EditText so that I can overwrite what happens when the back key is pressed while there is a keyboard on the screen. (Based on an answer to this linked question)
What I want to do when the back key is pressed is four things: set a boolean, change a custom EditText's text, change a Button's text, and change a TextView's visibility. All four of these are in a different file (which I believe may be what is causing the problem). But I can find them, and adjust two of them without crashes.
public class NiceEditText extends EditText {
Context context;
public NiceEditText(Context contx, AttributeSet attrs){
super(contx,attrs);
this.context = contx;
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (ListActivity.addedTitle) ListActivity.addedTitle = false;
NiceEditText editItem = (NiceEditText) findViewById(R.id.editItem);
editItem.setText("");
Button niceButton = (Button) findViewById(R.id.niceButton);
// niceButton.setText(R.string.addItem);
TextView addingTitle = (TextView) findViewById(R.id.addingTitle);
// addingTitle.setVisibility(View.INVISIBLE);
}
return super.onKeyPreIme(keyCode, event);
}
}
The two commented lines cause the app to crash due to a NullPointerException. I wonder if this is because it could not find either the Button or the TextView, but it could find the NiceEditText and change it.
Q: How can I change the Button's text and the TextView's visibility?