I'm new to android and this is my first application, it seems fine to me but every time I pressed the calculate button it seems to stop unexpectedly and force close.
package com.test.simplecalc;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button myButton = (Button) findViewById(R.id.myButton);
final EditText firstNum = (EditText)findViewById(R.id.firstNum);
final EditText secondNum = (EditText)findViewById(R.id.secondNum);
final EditText finalNum = (EditText)findViewById(R.id.finalNum);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int num1 = 0;
try {
num1 = Integer.parseInt(firstNum.getText().toString());
} catch(NumberFormatException nfe) {
Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
}
int num2 = 0;
try {
num2 = Integer.parseInt(secondNum.getText().toString());
} catch(NumberFormatException nfe) {
Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
}
int num3 = num1 + num2;
finalNum.setText(num3);
}
});
}
}
It's likely that one of the two blocks is throwing something other than NumberFormatException. (My guess would be a NullPointerException in the call to toString().)
Try changing the caught exception in each case to Exception and see if this reveals the problem.
just try
num3.toString()
while printing the answer, and better to use textview for showing answer
Related
I'm tring to build an simple android game.
Users answer the questions, when the answer is correct, it is continue..
I want to add time control for each answer.
I tried to add handler function, but I didn't.
My Code;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class EasyGameActivity extends Activity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_easygame);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finishScreen();
}
}, 5000);
startGame();
}
private void startGame() {
// TODO Auto-generated method stub
Button b1 = (Button)findViewById(R.id.answer_one);
Button b2 = (Button)findViewById(R.id.answer_two);
Button b3 = (Button)findViewById(R.id.answer_three);
Button b4 = (Button)findViewById(R.id.answer_four);
Random number = new Random();
int first = number.nextInt(100)+1;
int second = number.nextInt(100)+1;
int answer = first + second;
int rnd1 = answer + 1;
int rnd2 = answer + 2;
int rnd3 = answer - 1;
final String a = Integer.toString(answer);
String b = Integer.toString(rnd1);
String c = Integer.toString(rnd2);
String d = Integer.toString(rnd3);
((TextView) findViewById(R.id.display)).setText(Integer.toString(first) + '+' + Integer.toString(second));
List<Button> buttons = Arrays.asList(b1, b2, b3, b4);
List<String> texts = Arrays.asList(a, b, c, d);
Collections.shuffle(texts);
int i = 0;
OnClickListener onClick = new OnClickListener() {
public void onClick(View view) {
Button button = (Button) view;
String value = (String) button.getText();
if(value == a) {
checkTrue();
} else {
finishScreen();
}
}
};
for(Button button : buttons) {
button.setText(texts.get(i++));
button.setOnClickListener(onClick);
}
}
private void checkTrue() {
score++;
((TextView) findViewById(R.id.score)).setText(Integer.toString(score));
startGame();
}
private void finishScreen() {
score = 0;
startActivity (new Intent("com.bsinternet.mathfast.RESTARTGAMESCREEN"));
finish();
}
}
How can I add time control. Thanks.
This bit of code doesn't look right
if(value == a) {
checkTrue();
} else {
finishScreen();
}
You should be using equals() to check for String equality. At the moment you are checking only object equality, which will evaluate to False, and the code will never call checkTrue().
Do this instead:
if(value.equals(a) {
checkTrue();
} else {
finishScreen();
}
I'm an Android noob and doing my best to understand it. I'm trying to make an apk just for fun, to understand things better and also, have a problem which I haven't yet resolved even after searching google a lot.
So, the problem is that I have an EditText which allows only numbers, and by inserting a number in there, it will get into a string and, from there will be intruduced in setProgress as 'the percentage'. At least, that's what I want it to do; because, after putting that value into string and try to get it to be read as a percentage in setProgress, eclipse won't let me:
The method setProgress(int) in the type ProgressBar is not applicable for the arguments (String)
Here is the code:
*don't bother with anything else, it's just for testing...
package com.example.singur;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements OnClickListener
{
Button button;
ToggleButton toggle;
TextView rateText;
TextView textProgress;
Switch legit;
EditText edit;
RatingBar rating;
ProgressBar progress;
int count = 0;
// int value = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
toggle = (ToggleButton) findViewById(R.id.toggle);
rateText = (TextView) findViewById(R.id.rateText);
textProgress = (TextView) findViewById(R.id.textProgress);
legit = (Switch) findViewById(R.id.legit);
edit = (EditText) findViewById(R.id.edit);
rating = (RatingBar) findViewById(R.id.rating);
progress = (ProgressBar) findViewById(R.id.progress);
legit.setEnabled(false);
edit.setEnabled(false);
button.setOnClickListener(this);
toggle.setOnClickListener(this);
edit.setOnClickListener(this);
//rating.setOnRatingBarChangeListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button:
textProgress.setText("button clicked" + count + progress.getProgress());
count++;
progress.setProgress(count);
break;
case R.id.rating:
break;
case R.id.toggle:
if(toggle.isChecked() == true)
{
edit.setEnabled(true);
edit.setText("" + count);
//count++;
//progress.setProgress(count);
textProgress.setText("button clicked" + count + progress.getProgress());
}else
{
edit.setEnabled(false);
edit.setText("disabled but count = " + count);
//count++;
//progress.setProgress(count);
textProgress.setText("button clicked" + count + progress.getProgress());
}
count++;
textProgress.setText("toggle:"+count);
break;
case R.id.edit:
if(edit.isEnabled() == true)
{
edit.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
// do your stuff here
String value = edit.getText().toString();
progress.setProgress(value.valueOf(value));
}
return false;
}
});
}
}
}
}
You need to convert your String to an int. As the error indicates, setProgress() only accepts ints, whereas the value you get from the EditText is a String (even if the String can only contain numbers).
The easiest way to do this is with Integer.parseInt(String).
You code will look something like this:
String value = edit.getText().toString();
progress.setProgress(Integer.parseInt(value));
Also note that Integer.parseInt() will throw a NumberFormatException if the String can't be parsed as an Integer. Be sure to handle that Exception appropriately.
Hey guys I know there are many java programs for the factorial of a number...but I am facing problem with android. Following is my code....Thanks
package com.droidacid.apticalc.aptitudes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.droidacid.apticalc.R;
public class AptiFactorial extends Activity implements android.view.View.OnClickListener{
EditText number;
TextView answer;
Button calculate;
long factorial = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apti_factorial);
initialize();
calcFactorial();
}
private void initialize() {
number = (EditText) findViewById(R.id.et_apti_number);
answer = (TextView) findViewById(R.id.tv_apti_answer);
calculate = (Button) findViewById(R.id.b_apti_calc);
calculate.setOnClickListener(this);
}
private void calcFactorial() {
if (number.getText().toString().equals("")) number.setText("0");
int num = Integer.parseInt(number.getText().toString());
for(int i = 1; i<=num; i++){
factorial = i * factorial;
}
}
#Override
public void onClick(View v) {
calcFactorial();
answer.setText("Factorial of " + number.getText().toString() + " is : " + factorial);
}
}
This is my code and I need to know a way around for setting the hint instead of 0, but if I remove if (number.getText().toString().equals("")) number.setText("0");
then getting NullPointerException...
Also on the first go its calculating correctly but If I calculate again then getting wrong answer. I think some loop issue because I am directly using the value of factorial.
Please help thanks
Forget all that equal stuff, cover all basis and do it like this:
package com.droidacid.apticalc.aptitudes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.droidacid.apticalc.R;
public class AptiFactorial extends Activity implements android.view.View.OnClickListener{
EditText number;
TextView answer;
Button calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apti_factorial);
initialize();
}
private void initialize() {
number = (EditText) findViewById(R.id.et_apti_number);
number.setHint("Enter number to be factorialized :P")
answer = (TextView) findViewById(R.id.tv_apti_answer);
calculate = (Button) findViewById(R.id.b_apti_calc);
calculate.setOnClickListener(this);
}
private long calcFactorial() {
long factorial = 1;
try {
factorial = Long.parseLong(number.getText().toString());
for(int i=factorial-1; i>0; i--){
factorial = i * factorial;
}
} catch (NumberFormatException e) {
Toast.makeText(this, "Incorrect Input", Toast.LENGTH_LONG).show();
} finally {}
return factorial;
}
#Override
public void onClick(View v) {
answer.setText("Factorial of " + number.getText().toString() + " is : " + calcFactorial());
}
Do something like.
private void calcFactorial() {
int num = 0;
if (!number.getText().toString().equals(""))
num = Integer.parseInt(number.getText().toString());
for(int i = 1; i<=num; i++){
factorial = i * factorial;
}
}
Rather than using
(number.getText().toString().equals("")) number.setText("0");
Use
(number.getText().length==0) number.setText("0");
** first go its calculating correctly but If I calculate again then getting wrong answer**
try like this
private void calcFactorial() {
factorial = 1;///Check out the line..
int num = 0;
if (!number.getText().toString().equals(""))
num = Integer.parseInt(number.getText().toString());
for(int i = 1; i<=num; i++){
factorial = i * factorial;
}
}
You need to make sure variable factorial is reinitialise to 1 .. for getting factorial for next number..
private void fact_isClicked(View view) {
if(textField.length()>0){
int ifac = Integer.parseInt(textField.getText().toString());
int fact = factorial(ifac);
tvsec.setText(String.valueOf(fact));
textField.setText(ifac+"!");
}}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
This is the .java file for a small application I am writing in Android through Eclipse, and I am having a minor error or syntax glitch.
at the end bracket marked by asterisks, eclipse is reporting the error 'Syntax error, insert ";" to complete Statement'. I have searched the code, and find nothing unaccounted for or out of place. Could someone please identify or tell me how to fix this? If you need other files, tell me in the comments. Thanks in advance :)!
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
}
}
The compiler is right. You need to replace
**}** // Here is where the error occurs
with
});
to finish the btnAdd.setOnClickListener method call.
This is a statement in the onCreate(xx) and its an anonymous class, because an anonymous class is a statement you MUST terminate the statement:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
TO:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
Since you are using an annonymous inner class, you need to end the setOnClickListener method call with ");" as follows:
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}
You started a function call: setOnClickListener(). You need to finish it. Like so:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
If you indented properly, maybe you'd see it, but you're missing an extra parenthesis and semicolon:
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}
i have searched all over the place and havent been able to find the answer.
here is the code :
package hardy.scl;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class zody extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button b = (Button)this.findViewById(R.id.Button01);
final ImageView iv=(ImageView)this.findViewById(R.id.ImageView01);
final EditText et = (EditText)this.findViewById(R.id.EditText01);
final EditText et2 = (EditText)this.findViewById(R.id.EditText02);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int dd = Integer.parseInt(et.getText().toString());
int mm = Integer.parseInt(et2.getText().toString());
if (dd>=21&&mm==1||dd<=19&&mm==2){
iv.setImageResource(R.drawable.aq);
}
else if (dd>=20&&mm==2||dd<=20&&mm==3) {
iv.setImageResource(R.drawable.pi);
}
else if (dd>=21&&mm==3||dd<=20&&mm==4) {
iv.setImageResource(R.drawable.aries);
}
else if (dd>=21&&mm==4||dd<=21&&mm==5) {
iv.setImageResource(R.drawable.tau);
}
else if (dd>=22&&mm==5||dd<=21&&mm==6) {
iv.setImageResource(R.drawable.gem);
}
else if (dd>=22&&mm==6||dd<=22&&mm==7) {
iv.setImageResource(R.drawable.can);
}
else if (dd>=23&&mm==7||dd<=21&&mm==8) {
iv.setImageResource(R.drawable.leo);
}
else if (dd>=22&&mm==8||dd<=23&&mm==9) {
iv.setImageResource(R.drawable.virg);
}
else if (dd>=24&&mm==9||dd<=23&&mm==10) {
iv.setImageResource(R.drawable.lib);
}
else if (dd>=24&&mm==10||dd<=22&&mm==11) {
iv.setImageResource(R.drawable.sco);
}
else if (dd>=23&&mm==11||dd<=22&&mm==12) {
iv.setImageResource(R.drawable.sag);
}
else if (dd>=23&&mm==12||dd<=20&&mm==1) {
iv.setImageResource(R.drawable.cap);
}
if ((et.getText().length()<1)|| (et2.getText().length()<1)|| (et.getText().length()<1)&& (et2.getText().length()<1)){
Toast.makeText(getApplicationContext(), "Oh! invisible D.O.B..nice!", Toast.LENGTH_LONG).show();}
else{
Toast.makeText(getApplicationContext(), "ERRrr! Wrong D.O.B :/", Toast.LENGTH_LONG).show(); }
}});}}
what is it that im doin wrong that is causing the app to force close whenever i click the button if both or any of the edittexts are empty?
Your problem is when calling Integer.parseInt(). If the input is "" then Integer.parseInt throws NumberFormatException. You need to catch that explicitly because it is a runtime exception.
Just a guess since you didn't post your logcat, but if the edittexts are empty
int dd = Integer.parseInt(et.getText().toString());
int mm = Integer.parseInt(et2.getText().toString());
You can't parse an int out of an empty string.
I had the same type of problem and Amir was right on! I added the code for the try and catch here encase someone doesn't know how to catch a NumberFormatException
try {
}
catch (NumberFormatException e) { }