Hi! I a beginner in Android development. I have created a simple Hello World App in Android using Eclipse
IDE. It worked fine. Now I am creating a Jumbled Words game application. In this application there is View which has a TextView, an EditText and two Buttons. The first TextView will have a scrambled
word for example 'zzuple' for which the user has to guess and type the correct answer which is 'puzzle'. The two Buttons are Check and New Word respectively that checks the answer and displays a new word. If the user clicks on the Check button the message would be displayed as "Correct" if the answer provided by the user is correct or "Wrong" if the answer is wrong by the same. This works fine upto here. What I want is that when I click on the New Word the application must show me a new word which it retrieves from an array of scrambled words. How to do this? This means the text view must change its text dynamically on the click of the new word button. Rather my application displays a NullPointerException in the DDMS window of the Console. I have written the following code for this:
JumbledWords.java
package com.game;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class JumbledWords extends Activity {
/*declaring a list of actual words*/
private static final
String[] WORD_LIST =
{
"brazil","shirt","argentina","dynamic","internet"
};
/*declaring a list of scrambled words*/
private static final String[] SCRAMBLED_WORD_LIST =
{
"libraz","hirst","ntinaegra","ydmiacn","nietnret"
};
int wordIdx = 0; //Declares the word index
/*Declare the user interface components*/
TextView tvScrambledWord;
EditText etGuessedWord;
Button btnCheck, btnNewWord;
/*gets the value of the actual word*/
public static String getWord(int idx)
{
return WORD_LIST[idx];
}
public static String getScrambledWord(int idx)
{
return SCRAMBLED_WORD_LIST[idx];
}
/*gets the size of the actual words array*/
public static int getSize()
{
return WORD_LIST.length;
}
/*checks for the validity of the answer given by the user*/
public static boolean isCorrect(int idx, String userGuess)
{
return userGuess.equals(getWord(idx));
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvScrambledWord = (TextView)findViewById(R.id.txtScrambledWord);
etGuessedWord = (EditText)findViewById(R.id.txtGuessedWord);
btnCheck = (Button)findViewById(R.id.btnCheck);
btnNewWord = (Button)findViewById(R.id.btnNewWord);
tvScrambledWord.setText(getWord(wordIdx));
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(isCorrect(wordIdx, etGuessedWord.getText().toString())){
new AlertDialog.Builder(JumbledWords.this).setMessage("Awesome!!!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create().show();
}
else
{
new AlertDialog.Builder(JumbledWords.this).setMessage("Wrong. Try Again")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create().show();
}
}
});
btnNewWord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wordIdx++;
tvScrambledWord.setText(getScrambledWord(wordIdx));
etGuessedWord.setText("");
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/strInstruction"
android:textSize="20sp"
android:layout_margin="10dp"/>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/strEmpty" android:editable="false" android:id="@+id/txtScrambledWord"></EditText>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="@string/strEmpty" android:id="@+id/txtGuessedWord"></EditText>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/btnCheck" android:text="@string/strCheck"
android:textSize="20sp" android:layout_margin="10dp"></Button>
<Button android:text="@string/strNewWord" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:textSize="20sp" android:layout_margin="10dp" android:id="@+id/btnNewWord"></Button>
</LinearLayout>
Exception Message:
[2011-05-24 17:07:41 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:671)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
[2011-05-24 17:07:41 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:671)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
Please Help!!!