I need to change the texto de un boton
according to certain conditions, but this inside a Fragment
, but when I try to do it I have an error here:
mHexBtn.setText(modoConsola);
Could someone tell me how to correct this.
Here is my code:
private boolean mConfigModeConsola;
private String modoConsola = "";
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_main, container, false);
mSendText = view.findViewById(R.id.send_text);
mHexBtn = (Button) view.findViewById(R.id.hex_btn);
mHexBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if(mConfigModeConsola == false) {
mConfigModeConsola = true;
modoConsola = "HEX";
mHexBtn.setText(modoConsola);
mSendText.setText( "" );
}
else {
mConfigModeConsola = false;
modoConsola = "ASCII";
mHexBtn.setText(modoConsola);
mSendText.setText( "" );
}
}
});
return view;
}
Here the error:
I've been going through that code, the only things I don't see that are in your question is the button declaration, and I'm assuming the other id you get back is a
TextView
, because you didn't cast with(TextView)
I don't know if it is or not. I also do not see the blank constructor that requires theFragment
, maybe it is not very relevant for this case but it is important that you put most of the code to receive better answers. Here is the way I did the example, it works normally, it changes the text of the button every time I press it without problems.If it is the same as this and it still gives you the same error, I could recommend you to do is:
File > Invalidate Caches / Restart
You may have the error because of that, also clean the project
(Build > Clean Project)
and rebuild(Build > Rebuild Project)
, also sync withGradle
.mHexBtn.setText(consolemode);
consolemode is a string or where did you define it?
Could you put the error that you get?