从字符串中获取数据,而不是从文本中获取随机选项



我有一个关于使用字符串而不仅仅是文本的问题。我想用字符串代替文本,使翻译更容易。所以当我使用:

taken f02 = new taken("tegd")

的工作,但当我实现代码:

taken f01 = new taken(getString(R.string.proberen));

应用程序保持关闭,没有一个错误消息。有经验的人可以帮我解决这个问题吗?:)

public class Activity2 extends AppCompatActivity {
private TextView mTextView;
private Button mbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
mTextView = (TextView) findViewById(R.id.taken);
mbutton = (Button) findViewById(R.id.taken_button);
showRandom();
mbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRandom();
}
});
}
public void showRandom(){
shuffletaken();
mTextView.setText(TakenArray[3].getMtaken());
}
taken f01 = new taken(getString(R.string.proberen));
taken f02 = new taken("tegd");
taken f03 = new taken("test");
taken f04 = new taken("xcz");
taken f05 = new taken("dvg");

taken [] TakenArray = new taken[]{
f01,f02,f03,f04,f05};
public void shuffletaken() {
Collections.shuffle(Arrays.asList(TakenArray));
}
}
}
public class taken {
private String mtaken;
public taken(String mtaken){
this.mtaken = mtaken;
}
public String getMtaken(){
return mtaken;
}
}
<resources>
<string name="app_name">challenges</string>
<string name="test">test</string>
<string name="a">a</string>
<string name="b">b</string>
<string name="ct">c</string>
<string name="d">d</string>
<string name="proberen">test</string>
</resources>

您在类声明中使用了getString()方法,因此活动尚未附加到上下文。你必须在onCreate()方法中或之后使用getString()。

相关内容

  • 没有找到相关文章

最新更新