我真的无法使其工作..当“ checkLetter”开始运行应用程序崩溃时,我正在尝试做最简单的事情,但失败了,我想将usersInput(是一个字符)与随机数进行比较word字符串,但应用程序立即死亡。.
我正在尝试查看用户输入的内容是否在单词中,是否想要将正确的TextView更改为猜出的字母,但是我失败了。
package com.example.sharprazzor.hangmanv2;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Array;
public class game extends AppCompatActivity {
TextView tv1, tv2, tv3, tv4, test;
TextView[] tvArray = new TextView[4];
EditText et1;
Button btn1;
ImageView iv1;
int count = 0;
int countWrong = 0;
int countRight = 0;
String word, userInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
tv1 = findViewById(R.id.gtv1);
tv2 = findViewById(R.id.gtv2);
tv3 = findViewById(R.id.gtv3);
tv4 = findViewById(R.id.gtv4);
test=findViewById(R.id.textView8);
et1 = findViewById(R.id.get1);
btn1 = findViewById(R.id.gbtn1);
iv1 = findViewById(R.id.giv1);
userInput = et1.getText().toString();
}
//Creates random word from the list
public void setRandomWord(View view) {
String words = " //Bunch of words here not important ";
String[] arrayWords = words.split(" ");
int randomNumber = (int) (Math.random() * arrayWords.length);
String randomWord = arrayWords[randomNumber];
word = randomWord;
tv1.setText("?");
tv2.setText("?");
tv3.setText("?");
tv4.setText("?");
for (int k=0; k<4; k++)
tvArray[k] = new TextView(this);
test.setText(word);
}
public void checkLetter(View view) {
char a = userInput.charAt(0);
for(int i=0; i<4; i++) {
if (userInput.equals(Character.toString(word.charAt(i)))) {
countRight++;
tvArray[i].setText(word.charAt(i));
}
}
}
}