Reputation: 57
I am making an android app which requires a button to perform two taks: 1. The button should show an Error message in a TextView when user supplies a wrong input in EditText.
My app is a kind of a simple game which contains 4 activities(activity_1, activity_2, activity_3, activity_4). It asks for an input in activity_2. If it gets a wrong input the it gives the Error message. But on providing the correct input the app crashes.
code activity_2.xml :
<Button
android:id="@+id/buttonClickMe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView7"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:onClick="sendMessage"
android:text="@string/button_start" />
code activity_2.java :
final Button buttonStart = (Button) findViewById(R.id.button1)
buttonStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int no = Integer.valueOf(numberField.getText().toString());
switch (no)
{
case 1 : maxRand = 15; // the random number will be between 0 and maxRand
break;
case 2 : maxRand = 30;
break;
case 3 : maxRand = 50;
break;
case 4 : maxRand = 100;
break;
default : textField.setText("Invalid Argument, Please try again.");
break;
}
if(maxRand != 0){
randNumber = (int) (Math.random() * maxRand);
/*textField.setText("Now click Start button !");
buttonStart.setVisibility(View.VISIBLE);*/
startActivity(intent); //This starts a new activity
}
}
});
and the code followed by the onClick method :
public void sendMessage(View view){ //This method is called everytime when the button is clicked
intent = new Intent(this,ShowResultsActivity.class);
}
Ive declared intent as the data member of the class.
Please do help me. And remember to provide bunch of code for reference.
Upvotes: 0
Views: 108
Reputation: 6905
public void sendMessage(View view){
//This method is called everytime when the button is clicked
int no = Integer.valueOf(numberField.getText().toString());
switch (no)
{
case 1 : maxRand = 15; // the random number will be between 0 and maxRand
break;
case 2 : maxRand = 30;
break;
case 3 : maxRand = 50;
break;
case 4 : maxRand = 100;
break;
default :
textField.setText("Invalid Argument, Please try again.");
// add this to ensure that you won't start activity
maxRand = 0;
break;
}
if(maxRand != 0){
randNumber = (int) (Math.random() * maxRand);
intent = new Intent(this,ShowResultsActivity.class);
startActivity(intent); //This starts a new activity
} else {
// maxRand == 0 ===> ERRORS, update your textField here
textField.setText("Invalid Argument, Please try again.");
}
}
Upvotes: 0
Reputation: 119
try initiating Intent in setOnClickListener itself
final Button buttonStart = (Button) findViewById(R.id.button1) buttonStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int no = Integer.valueOf(numberField.getText().toString());
switch (no)
{
case 1 : maxRand = 15; // the random number will be between 0 and maxRand
break;
case 2 : maxRand = 30;
break;
case 3 : maxRand = 50;
break;
case 4 : maxRand = 100;
break;
default : textField.setText("Invalid Argument, Please try again.");
break;
}
if(maxRand != 0){
randNumber = (int) (Math.random() * maxRand);
intent = new Intent(this,ShowResultsActivity.class);
startActivity(intent); //This starts a new activity
}
}
});
If it doesn't fix the problem. pate your logcat output here..
Upvotes: 1