When I get the string from the strings.xml
, I don't get an error right away, but I can't run the program :
String myButtonText = getString(R.string.ButtonText);
(when I remove this line and this variable from my program it works perfectly)
I'll put up my program, in case you'd need it:
public class MainActivity extends AppCompatActivity {
private Button button;
String myButtonText = getString(R.string.ButtonText);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(myButtonText.equals("Red")){//the button's text is "red" already
button.setText("Blue");
}
}//I'm changing the text on the button to "blue" when it says"red"
});
}
}