• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

TrueWords App

Hi everyone,

Just started reading through tutorials, etc. I have a problem with my code I can't find a solution to anyway, please could you provide an explanation as to its behaviour, I am interested to know why it does this.

______ My Code _______

public class Main extends Activity implements OnKeyListener {
DisplayWord currentWord;
TextView wordStatus;
EditText inputWord;
Button okBtn;
int pos = 1;
char currentChar;
char inputChar;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

currentWord = (DisplayWord) this.findViewById(R.id.trueWord);
currentWord.setText("Bonjure"); // word from database

wordStatus = (TextView) this.findViewById(R.id.wordStatus);

inputWord = (EditText) this.findViewById(R.id.wordInput);
inputWord.setOnKeyListener(this);
}

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (inputWord.getText().toString().length() <= currentWord.getText().toString().length()) {
pos = inputWord.getText().toString().length();
currentChar = currentWord.getText().toString().charAt(pos);
inputChar = inputWord.getText().toString().charAt(pos);

} return false;
}
}
_____ END _____

When running this code, upon entering data into the input box it causes an exception straight away and for the program exit unexpectedley. I am simply trying to extract the input word from EditText field and the word stored in a TextView and to compare them char by char, as those chars are entered into the input box, sort of a word game, where the character will be highlighted green if it is correct and red if it is not.

I have no idea why this behaviour happens, I can see in debugger an StringIndexOutOfBounds exception, and a ResourceNotFound when I have been tampering with different ways of attempting this, I'm fail and fail to see how such a simple program can elude me for so long.

Where am I going wrong?

Many thanks,
Anthony
 
Back
Top Bottom