ParseInt Exception - android

I am creating a small calc app with EditText views and Im running into an runtime exception when the user leaves an EditText view empty causing the ParseInt to try and Parse nothing. Ive read that I need to 'Try' and 'Catch' this error before it occurs, but Im unsure of where and how to do this!
Any advice is much appreciated!
Here is my code:
public class HandlerExamples extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v) {
String a,b,t;
double vis;
EditText txtbox1 = (EditText)findViewById(R.id.A);
EditText txtbox2 = (EditText)findViewById(R.id.B);
EditText txtbox3 = (EditText)findViewById(R.id.t);
TextView tv = (TextView) findViewById(R.id.Answer);
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
t = txtbox3.getText().toString();
vis = ((Integer.parseInt(a)*1) + (Integer.parseInt(b)*2)) / (Double.parseDouble(t));
tv.setText(double.toString(vis));
}
}
Thanks so much!

public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.xx:
//do things xx click
break;
case R.id.yy:
//do things yy click
break;
}
}
you can get the view id to know whick widget was clicked.

Changwei Yao defined one way you can do this, but here's the way most Android programmers would do this (programmatically), since it's a little easier to read and figure out what your widgets are doing:
But first, remove the implements OnClickListener from your Activity, as it's not needed.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your button to do when clicked
}
}
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your EditText to do when clicked
// (such as editText.setText(""))
}
}
Another way to do the same thing is to define android:onClick="insert_method_name_here" for the widgets that you want perform an action when clicked. In your case, in your main.xml (since that's what you're using in your Activity), you could write something like...
<Button android:id="#+id/testButton"
(other attributes you wish to apply to the button)
android:onClick="buttonAction" />
<EditText
(other attributes)
android:onClick="textAction" />
And then, in your Activity, you define the methods buttonAction(View v) and textAction(View v). Note that these methods must be public void, and must take the sole argument View v.
(One advantage of the XML method is that you don't necessarily have to define an android:id attribute for these widgets, unless you need to be able to manipulate them or extract information from them in your code (which means you will need to define an android:id for your EditText since you'll likely want the user's input))

If you only need to exclude the empty text field then hotveryspicy's solution is probably the quickest. For a secure solution: catching the NumberFormatException will filter anything that can not be converted to an integer.
int vis;
try {
vis = Integer.parseInt(a);
}
catch(NumberFormatException nfe) {
Log.e(TAG,"trying to convert:"+a+" to integer failed");
vis = 0;
}

Related

Android calculator buttons

I am trying to make a calculator for Android. Here is the code for my buttons:
int[] button_ids = {
R.id.BtnNum0, R.id.BtnNum1, R.id.BtnNum2, R.id.BtnNum3, R.id.BtnNum4, R.id.BtnNum5, R.id.BtnNum6,
R.id.BtnNum7, R.id.BtnNum8, R.id.BtnNum9, R.id.BtnAdd, R.id.BtnSub, R.id.BtnDiv, R.id.BtnMult,
R.id.BtnClear, R.id.equals
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditTextValue = (EditText) findViewById(R.id.editText1);
TVValue = (TextView) findViewById(R.id.textView1);
buttons = new ArrayList<Button>();
for(int id : button_ids) {
Button button = (Button)findViewById(id);
button.setOnClickListener(this);
buttons.add(button);
}
}
How I can change this part to a block of code where I won't have to declare the IDs of the buttons? (e.g. R.id.BtnNum0)
int[] button_ids = {
R.id.BtnNum0, R.id.BtnNum1, R.id.BtnNum2, R.id.BtnNum3, R.id.BtnNum4, R.id.BtnNum5, R.id.BtnNum6,
R.id.BtnNum7, R.id.BtnNum8, R.id.BtnNum9, R.id.BtnAdd, R.id.BtnSub, R.id.BtnDiv, R.id.BtnMult,
R.id.BtnClear, R.id.equals
};
I have been searching for an answer, but I still can't find a solution.
What you can do, since this code seems to only set a single OnClickListener for all Buttons, is to do it in xml
For each Button set
android:onClick="functionName"
then in your code you can do away with all of the id's and your for loop. In Java just create a function like
public void functionName(View v)
{
switch (v.getId())
{
case R.id.buttonId:
// do work for this Button
break;
...
}
The way you are doing it is fine but this is how I prefer to handle this situation. You just have to give all of the Buttons the same function name in xml then use that name as your function name in Java. You also just have to be sure to declare the function with a void return type and make sure it takes a View as its one and only parameter as in my example.
The Button Docs also have an example of this
in your layout file add this to every button
<Button
...
android:onClick="btnClicked"
.../>
then in your code add this method and check for each button in this method
public void btnClicked(View v)
{
switch(v.getId())
{
case R.id.BtnNum0:
// your code
break;
....
}
}
That is likely the best solution unfortunately, unless you use some sort of annotation framework which still doesn't cut down much on the boilerplate.
edit:
You could try to get a pointer to whatever ViewGroup is holding the Button views and then getting all of its children, and then looping through them while casting them to Buttons as you go.
For example: If your Button objects in XML are housed in a LinearLayout, you could get the pointer to that and do something like this:
for(int i=0; i < ((ViewGroup)v).getChildCount(); ++i) {
Button nextChild = (Button) ((ViewGroup)v).getChildAt(i);
}
Of course, I recommend against this, but it is still a possibility.
As trevor-e suggested, you can give an annotation processor a try. Android Annotations can simplify your code to:
#Click
public void BtnNum0() {
// Button 0 clicked
}
#Click
public void BtnNum1() {
// Button 1 clicked
}
// etc.
If you go this route, please do try to use names following the Java convention as the button names correspond with function names.

Dynamically populate a button value android

I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }
I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).
All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.
Hope that all makes sense.
UPDATE:
I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);
//set the title of the popup
TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
titletext.setText(activityName);
if (activityName.equals("Root"))
{
switch (arg0.getId())
{
case R.id.color:
//Rename the string so to get the value from the string xml
String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
break;
case R.id.polarity:
//Rename the string so to get the value from the string xml
String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
//polarityDesc.setText(activityName);
polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
break;
}
}
I think
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
should be
Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
Button btnOpenPopupSecond = (Button) findViewById(R.id.color);
you should declare different different button for diffrerent findviewbyid
also in my eclipse it is not accepting
btnOpenPopup.setOnClickListener(new Button.OnClickListener()
instead it works with
btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
and you need to provide more clear view of what you want to perform
new thoughts,try doing this:
btnOpenPopupFirst.setOnClickListener(this);
btnOpenPopupSecond.setOnClickListener(this);
then option will come on both the above code lines
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
choose this
let MainActivity implement OnClickListener
then this option will come
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
choose
add unimplemented method
now
#Override
public void onClick(View v)
will be created
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.polarity:
Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
break;
case R.id.color:
Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
default:
break;
}
}
And post your views after implementing this way.
int[] id={R.id.button1,R.id.button2};
Button b=(Button)findViewById(id[i]);
The onClick method in Button.OnClickListener has a View parameter... you can call getId() on that view to get the id of that button that was clicked on.
It doesn't make too much sense to me. If what you really want is this:
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
All you need to do is set your value in the onClick(View view) method of your OnClickListener
public void onClick(View view) {
btnOpenPopup = (Button)view;
}

How to display a something from an EditText?

I thought making thing part of the app would be easy, however I was wrong. I wish to have a textView display whatever the user wrote in the editText. This is what I tried.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myTextView.setText(myEditText.getText().toString());
// of course I would use variables in place of the
// myTextView and myEditText
}
});
This is another way I tried to get this done.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//num1 is my String variable
num1 = myEditText.getText().toString();
myTextView.setText(num1);
}
});
Both times the textView comes up with nothing in it.
Thank you for any help!
onClickListener merely responds to user clicks. You need to implement a TextWatcher on your EditText. The most straightforward way of doing this is to implement TextWatcher in your class, then make a call to myEditText.addTextChangedListener(this).
I recommend adding something like the following to your onTextChanged method:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
myTextView.setText(myTextView.getText()+s);//or something like this...
}
I usually use GetDlgItemText.
char Buffer[120];
GetDlgItemText(hwndDlg, (control), buffer, sizeof(buffer));
This will read it and store it in buffer.
In the EditText the getText call should you return the String, I don't believe you need to call the ToString method on it. The way you are using it in the onClickListener implies you have a button that should be calling a function to set the text into the textview. If you want it dynamically you should be able to use onTextChanged to fill in the data.
First of all check whether the control is coming to your setOnClickListener(). Put in a Log to find that out.
Next make sure that "add" is the button or item that u r using to initiate the copy process.
This statement of yours is correct.
myTextView.setText(myEditText.getText().toString());
Though you do not require the toString(). Doesnt really make a difference. I suggest you check that your textview and edittext is fine.
have you check the visibility of textview ?before clicking add button it is invisible rite?then u have to set the visibility on add button click.
From your code i understood that there is a button here too so try this should work:
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.mybutton);
btn.setOnClickListener(btncall);
}
private OnClickListener btncall = new OnClickListener()
{
public void onClick(View v)
{
TextView mytextView = (TextView) findViewById(R.id.MytextView);
EditText myeditText = (EditText) findViewById(R.id.MyeditText);
mytextView.setText(myeditText.getText().toString());
}
};
}

How to get the value of pressed button?

I am searching the solution, how to get the value of the button which is pressed.
When I try something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button numb1 = ((Button)this.findViewById(R.id.numb1));
numb1.setOnClickListener(this);
}
public void onClickHandler(View v){
String pressed = null;
switch (v.getId()) {
case R.id.numb1:
pressed=numb1.getText().toString();
break;
//OR
case R.id.numb1:
pressed=R.id.numb1.getText().toString();
break;
}
new AlertDialog.Builder(this).setTitle("Info").setMessage(pressed).setNeutralButton("Okey", null).show();
}
Both cases in switch are unfortunately bad.
And I still can't get the value of the pressed button... Can you help me please with this problem yet?
Thank you.
Well guys, I am pretty new in this area, but I solved this problem like that:
public void onClick(View view) {
int intID = view.getId();
Button button = (Button) findViewById(intID);
String message = button.getText().toString();
}
pressed=((Button)v).getText(); should do the job.
Also, let your activity implement View.OnClickListener and instead of onClickHandler() override the method public void onClickHandler(View v) with your implementation.
try this.
Button sender = (Button)v;
sender.text;
Looking at your code, is it possible you have two variables with the same name but different scopes causing you confusion?
In onCreate, you declare Button numb1, but onClickHandler appears to expect numb1 to have also been declared outside of onCreate. So, should onCreate just assign a value to numb1, instead of also declaring it?
At any rate, if you were to post a more complete code example, it may help others to identify the problem, instead of just guessing about how things are.

ClickListener current target

EditText etHomePhone = (EditText)findViewById(R.id.et_pi_home_phone);
EditText etMobilePhone = (EditText)findViewById(R.id.et_pi_home_phone);
etHomePhone.setOnClickListener(showPopUpClickListener);
etMobilePhone.setOnClickListener(showPopUpClickListener);
private View.OnClickListener showPopUpClickListener = new View.OnClickListener() {
public void onClick(View v) {
/* I like to get both EditText.getText().toString() value in this one ClickListener
is it Possible, there is something in ActionScript call event.currentTarget...
*/
}
};
If you declare your etHomePhone and etMobilePhone variables as final and define your OnClickListener inline in the same method, then you can refer to those variables directly. Like so:
final EditText etHomePhone = (EditText)findViewById(R.id.et_pi_home_phone);
final EditText etMobilePhone = (EditText)findViewById(R.id.et_pi_home_phone);
View.OnClickListener showPopUpClickListener = new View.OnClickListener() {
public void onClick(View v) {
String home = etHomePhone.getText().toString();
String mobile = etMobilePhone.getText().toString();
// Do something with home and mobile
}
};
etHomePhone.setOnClickListener(showPopUpClickListener);
etMobilePhone.setOnClickListener(showPopUpClickListener);
EDIT:
If you only want to get the text for the EditText that was clicked instead of both of them, then you can just cast the View that it delivered via onClick(View v):
public void onClick(View v) {
EditText editText = (EditText)v;
String phoneNumber = editText.getText().toString();
// Do something with phoneNumber
}
This is a snippet, so I can't see the context but you can structure a single OnClickListener to catch all events in your activity. First, your activity will need to implement the OnClickListener interface:
public class YourActivity extends Activity implements OnClickListener {...
In the OnCreate() method, register each UI element which should respond to clicks like this:
yourObject.setOnClickListener(this);
Next, for the activity's onClick() method, create a switch structure using R.id like this:
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.A_UI_Element:
//do what you need for this element
break:
case R.id.A_Different_UI_Element:
//do what you need for this element
break;
//continue with cases for each element you want to be clickable
}
}
Don't forget the break at the end of each case.
One thing I noticed in your code: both editText declarations refer to the same resource. This gives you two handles to the same UI element, not 2 different elements.

Categories

Resources