我是Android编程新手。我需要一个启发我的Android应用程序。我不知道什么是错的,但当我按下我的"计算"按钮,它将停止工作(崩溃)。我的程序是关于FLAMES游戏的,你在每个实例中计算一个字符串的字符在另一个字符串中相交。我希望了解更多,每一个帮助将不胜感激。谢谢!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText edittext1 = (EditText) findViewById(R.id.editText);
final String name1 = edittext1.toString().replaceAll("\\s+","");
EditText edittext2 = (EditText) findViewById(R.id.editText2);
final String name2 = edittext2.toString().replaceAll("\\s+","");
final TextView result = (TextView) findViewById(R.id.textView);
int common = 0;
int resultS = common % 6;
if (name1.length() > name2.length()){
for (int i = 0; i < name1.length(); i++) {
for (int j = 0; j < name2.length(); j++) {
if (name1.charAt(i) == name2.charAt(j)) {
common++;
}
result.setText(resultS);
}
}
}
if(name2.length() > name1.length())
{
for (int i = 0; i < name2.length(); i++) {
for (int j = 0; j < name1.length(); j++) {
if (name2.charAt(i) == name1.charAt(j)) {
common++;
}
result.setText(resultS);
}
}
}
}
});
logcat的:
07-09 14:53:06.852 8302-8302/com.example.genesis.flames E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.genesis.flames, PID: 8302
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:338)
at android.widget.TextView.setText(TextView.java:5480)
at com.example.genesis.flames.MainActivity$1.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:6219)
at android.view.View$PerformClick.run(View.java:24482)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答案 0 :(得分:2)
result.setText(String.valueOf(resultS));
因为textview期望一个字符串,你将参数作为整数发送
也result.setText(""+resultS);
应该有效(这个没有经过测试)