當我從strings.xml
字符串,我沒有得到一個錯誤的時候了,但我不能運行程序:從資源的strings.xml獲取字符串
String myButtonText = getString(R.string.ButtonText);
(當我刪除此行,這個變量從我的程序它完美的作品)
我要是把我的程序,如果你需要它:
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"
});
}
}
從類的頂部移除出處,並做到這一點的onCreate方法裏面:'myButtonText = getResources()的getString(R.string.ButtonText)' – joao86