Unknown Error - Integer Parsing - java

My ultimate goal for this app is to make a sort of "tip book" for waiters/waitresses so they can record their tips they make each day. The app will display amount made, hours worked and hourly amount. It will also have an option to review all previous work days, showing daily, weekly and monthly totals.
I am trying to take the numbers input into a two EditText boxes in the main.xml file, id etTip and etHour, display them in two TextView, tvTip and tvHour, then divide the number in etTip by etHour to get the wage, which would then be displayed in tvWage after pressing the Submit button, bSubmit.
The tips and hours display themselves easily enough, no errors found when all the app is doing that, but when I attempt to use the Integer.parseInt(String string) command, I get an error message. If the parsing is done during the onCreate method, the error is immediate after opening the app. If the parsing is done after pressing the button, the app force closes quickly after pressing the button.
I've located the problem, but I just don't know how to go about fixing it.
Below is the main xml file:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/etTip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:layout_weight="50"
android:layout_margin="8dp"
android:hint="Tips">
<requestFocus />
</EditText>
<EditText
android:id="#+id/etHour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:layout_weight="50"
android:layout_margin="8dp"
android:hint="Hours" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/bSubmit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/btSubmit"
android:textSize="35sp"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tvTip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="50"
android:text="Tips"
android:gravity="center"/>
<TextView
android:id="#+id/tvHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="50"
android:text="Hours"
android:gravity="center"/>"
</LinearLayout>
<TextView
android:id="#+id/tvWage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Wage"
android:gravity="center"/>
</LinearLayout>
And the main Activity:
package com.smarticle.tipbook;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
int tip,wage,hour;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
tip = Integer.parseInt(textTip.getText().toString());
hour = Integer.parseInt(textHour.getText().toString());
Button bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textTip.setText(editTip.getText());
textHour.setText(editHour.getText());
wage = tip / hour;
textWage.setText(wage);
}
});
}
}

It's just because you parsed to int your edit text content at load which is why the first value is null which is why it can't be parsed into integer. That is why I just placed the parse part inside the onClick for the content of the editview to be converted after clicking. Also change your edit view to accept only numeric so that it won't fail in case a user input an invalid characters. :) There you go.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
//int tip = Integer.parseInt((String) textTip.getText());
//int hour = Integer.parseInt((String) textHour.getText());
Button bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textTip.setText(editTip.getText());
textHour.setText(editHour.getText());
wage = Integer.parseInt(textTip.getText().toString()) / Integer.parseInt(textHour.getText().toString());
textWage.setText(String.valueOf(wage));
}
});
}
}

Use double instead:
double tip,wage,hour;
And,
tip = parseDouble(textTip.getText().toString());
hour = parseDouble(textHour.getText().toString());
Also, you should check that the input in EditText is actually a number before parsing. And you should enclose the parsing in try/catch block.

Related

Making a calculator in android. The app shows up on the emulator with no errors but when I press the calculate button it crashes

I'm new to this and I'm trying to follow some code from another answer. I have three text fields to enter the numbers that are used for the calculation and a button below that is supposed to change some text below it to the answer. There are no errors shown when I run the app but when I press the button the app crashes and I get error messages in the log. This is the code:
public class CalculatorActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
class NewActivity extends CalculatorActivity {
//Declare textviews as fields, so they can be accessed throughout the activity.
EditText editTextSubsPrevious;
EditText editTextSubsNow;
EditText editTextDays;
TextView calcText;
Button calcButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
//Bind the EditText views
editTextSubsPrevious = (EditText) findViewById(R.id.editTextSubsPrevious);
editTextSubsNow = (EditText) findViewById(R.id.editTextSubsNow);
editTextDays = (EditText) findViewById(R.id.editTextDays);
calcButton = (Button) findViewById(R.id.calcButton);{
View.OnClickListener onClickListener = new Button.OnClickListener() {
#Override
public void onClick(View v) {
//When the button is clicked, call the calculate method.
calculate();
}
};
}
}
public void calculate(){
//get entered texts from the edittexts,and convert to integers.
Double value1 = Double.parseDouble(editTextSubsPrevious.getText().toString());
Double value2 = Double.parseDouble(editTextSubsNow.getText().toString());
Double value3 = Double.parseDouble(editTextDays.getText().toString());
//do the calculation
Double difference = (value2 - value1) / 7;
Double percent = (difference / 325);
Double amount;
amount = value2 * Math.pow(1 + percent, value3);
calcText.setText(amount.toString());
}
}
}
}
Here is the error message:
FATAL EXCEPTION: main
Process: com.pausetheapp.futurefollowerandsubscribercalculator, PID: 3420
java.lang.IllegalStateException: Could not find method sendMessage(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'calcButton'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Here is the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.pausetheapp.futurefollowerandsubscribercalculator.CalculatorActivity"
tools:showIn="#layout/activity_calculator">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/textViewTitle"
android:id="#+id/textViewTitle"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editTextSubsPrevious"
android:layout_marginTop="28dp"
android:layout_below="#+id/textViewTitle"
android:layout_centerHorizontal="true"
android:hint="#string/previousSubsTextField"
android:width="300dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editTextSubsNow"
android:layout_below="#+id/editTextSubsPrevious"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:width="300dp"
android:hint="#string/currentSubsTextField" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editTextDays"
android:hint="#string/daysTextField"
android:width="300dp"
android:layout_marginTop="30dp"
android:layout_below="#+id/editTextSubsNow"
android:layout_alignLeft="#+id/editTextSubsNow"
android:layout_alignStart="#+id/editTextSubsNow" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/accuracyText"
android:id="#+id/textViewAccuracy"
android:layout_below="#+id/editTextDays"
android:layout_alignLeft="#+id/editTextDays"
android:layout_alignStart="#+id/editTextDays"
android:textSize="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calculateButton"
android:id="#+id/calcButton"
android:layout_marginTop="42dp"
android:layout_below="#+id/textViewAccuracy"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/calcDays"
android:id="#+id/calcText"
android:layout_marginTop="22dp"
android:textSize="25dp"
android:layout_below="#+id/calcButton"
android:layout_centerHorizontal="true"
android:gravity="center" />
I just added this line to the java file.
calcText = (TextView) findViewById(R.id.calcText);
There are two possibilites of error
1> You have not defined calcText
calcText.setText(amount.toString());
2> Or May be
Double value1 = Double.parseDouble(editTextSubsPrevious.getText().toString());
Double value2 = Double.parseDouble(editTextSubsNow.getText().toString());
Double value3 = Double.parseDouble(editTextDays.getText().toString());
editTextSubsPrevious,`editTextSubsNow`,`editTextDays` => does not contain numbers so that it can be parsed to `Doubles`
Probably, you have following attribute in your layout XML file:
android:onClick="sendMessage"
Then, when you click, android search for method sendMessage(View view) and it does not find (because you did not implemented)
Could not find method sendMessage(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'calcButton'
So, since your are setting a onClickListener at runtime, I think you can delete android:onClick="sendMessage" from layout file.
Also, line below should be fixed as follows:
calcButton = (Button) findViewById(R.id.calcButton);
calcButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//When the button is clicked, call the calculate method.
calculate();
}
});
In your layout most probably you have assigned sendMessage() method to handle onClick event and have not created any such method in activity.
check for android:onClick="sendMessage" in your layout.
Either create a method sendMessage(View view) and write your code there or remove it and use your calcBtn.setOnClickListner(View.OnClickListner Object).
If your app crashes in response to a button click it could be due to the following:
The function you registered for the onClick attribute of the button
in the layout .xml file does not exist in the activity. You can make sure the function name in the onClick attribute matches the function name you mean to call in your activity.
Something in the onClick function causes an Exception. If this is the case you can use the debugger by setting a breakpoint inside
that function and inspecting your code and values while you step
through the program. Debug Your App
if u r using this
calcButton = (Button) findViewById(R.id.calcButton);{
View.OnClickListener onClickListener = new Button.OnClickListener() {
#Override
public void onClick(View v) {
//When the button is clicked, call the calculate method.
calculate();
}
};
}
u should use
android:onClick="onClick"
else u need to use
calcButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//When the button is clicked, call the calculate method.
calculate();
}
});

Creating a Calculator Android app [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm learning android programing and i just dont know how turn my xml into a working calculator.
here is my xml, i removed the styling for better reading
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.afm.calculator.MainActivity" >
<TextView
android:id="#+id/tvresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="40dp" />
<Button
android:id="#+id/times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:textSize="40sp" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40dp" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="รท"
android:textSize="40sp" />
<Button
android:id="#+id/mines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40sp" />
<Button
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textSize="40sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
here is my java:
package com.afm.cal;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
Button mines, plus, divide, times, equal;
EditText eto, ett;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mines = (Button) findViewById(R.id.mines);
plus = (Button) findViewById(R.id.plus);
divide = (Button) findViewById(R.id.divide);
times = (Button) findViewById(R.id.times);
equal = (Button) findViewById(R.id.equal);
eto = (EditText) findViewById(R.id.editText1);
ett = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.tvresult);
}
public void onClick(View arg0) {
}
}
please help me finish it and thx in advance
First add Listener to all your buttons using setOnClickListener method.
Eg: plus.setOnClickListener(this);
Make modifications in your onClick method something like below.
public void onClick(View view) {
if (view == mines) {
// do the calculation here
//update the TextView using tv.setText()
} else if (view == plus) {
// do the calculation here
//update the TextView
}
//.. So on for all buttons
}
Assign each button a method using android:onClick attribute of Button in xml.
For example android:onClick="doSomething" . This button will call the doSomething() method when it is clicked.
Your methods should get values from TextFields and do the operation inside these methods.Then display the result in TextView

Android: Unfortunately <app name> has stopped

I'm new to android development, this is just my second app i built just to kill time, though, i could'nt understand, what caused this error. Need expert advice
The activity_starting_point.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".StatingPoint" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Enter Number 1"
android:id="#+id/tvDisplay"
/>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Enter Number 2"
android:id="#+id/tvDisplay"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add"
android:layout_gravity="center" />
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Your Total is 0 "
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
</LinearLayout>
The Java class file: StartingPoint.java
package com.ankur.calulator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class StatingPoint extends Activity {
int num1,num2;
int total;
Button add,sub;
TextView display;
EditText no1,no2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stating_point);
total=0;
no1=(EditText) findViewById(R.id.editText1);
no2=(EditText) findViewById(R.id.editText2);
display=(TextView)findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
total=num1+num2;
display.setText("Your Total is "+total);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
total=num1-num2;
display.setText("Your Total is "+total);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_stating_point, menu);
return true;
}
}
The logcat file:
its got blank, i think i messed up with it, though i saw something as java null pointer exception before
i knw i'm close, just nt able to find some missing link, need advice.
You haven't initialized add and sub like you do for no1,no1 and display
And you are trying to call setOnClickListener on add and sub without initializing so you get a NullPointerException
You need to provide different id to add and sub and initialize it.
In your activity_starting_point.xml, you have:
3 TextViews with the same id: tvDisplay
2 Buttons with the same id: button1
Please assign unique AND meaningful IDs to be able to reference them later in your java code, eg:
android:id="#+id/btAdd"
and
android:id="#+id/btSubtract"
(NOTE: you actually don't need id for TextViews with instructions, eg. all those tvDisplay, unless you want to actually reference them in Java, for example to change the instruction text)
Next, as spotted by Apoorv, whereas you initialize your no1 and no2, you don't do it with add and sub, hence they are null. Just initialise them the same way the same way as the above:
add = (Button) findViewById(R.id.btAdd);
and
sub = (Button) findViewById(R.id.btSubtract);
Also, in your OnClickListeners, your calculations will always yield 0 for total, because nowhere in the code you're actually getting values for num1 and num2. You probably want to do something like:
num1 = Integer.parseInt(no1.getText().toString());
num2 = Integer.parseInt(no2.getText().toString());

Radiobutton not working (simple code)

Well, the issue is... my code is supposed to output the amount from the radiobutton clicked by the user, but it's not outputting the amount at all... I'm really not sure what the problem is. I created a radiogroup and used if-statements to determine if a radiobutton was checked or not. I think the issue is somewhere in calculating the amount.
Do I need to make a button? I thought it would calculate the amount automatically with the if-statements when a radiobutton were clicked. If I need a button, how would I go about creating one? (I'm trying to avoid using listener for now)
I'm a complete noob at android/java. I really would appreciate it if someone could explain what's wrong with my code. Thanks!
JAVA CODE
package com.Rox.crazyjoe;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final double smallcst = 1.25;
final double mediumcst = 2.00;
final double largecst = 3.50;
double amount;
RadioButton small = (RadioButton) findViewById(R.id.radioSmall);
RadioButton medium = (RadioButton) findViewById(R.id.radioMedium);
RadioButton large = (RadioButton) findViewById(R.id.radioLarge);
if (small.isChecked())
{
amount = smallcst;
}
else if(large.isChecked())
{
amount = largecst;
}
else if(medium.isChecked())
{
amount = mediumcst;
}
else
{
amount = 0;
}
DecimalFormat money = new DecimalFormat("$##.00");
TextView t0 = (TextView)findViewById(R.id.textAmount);
t0.setText("Total Amount: " + money.format(amount));
}
}
XML CODE:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="Size: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:text="Small" />
<RadioButton
android:id="#+id/radioMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioButton1"
android:layout_below="#+id/radioButton1"
android:text="Medium" />
<RadioButton
android:id="#+id/radioLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioMedium"
android:layout_below="#+id/radioMedium"
android:text="Large" />
</RadioGroup>
<TextView
android:id="#+id/textAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="40dp"
android:layout_marginRight="54dp"
android:text="TextView" />
</RelativeLayout>
You will need an event to trigger the checking of the radio buttons. At the moment, it is just checking the if statement in the onCreate and that's it (if you had a radiobutton checked by default, a value would be set here). I recommend attaching a listener to the entire RadioGroup
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
//other logic here (like what do you want to do with this selected RadioButton)
}
});

My android app isn't displaying anything when i add numbers to it's text field

I'm new to programming for the android and I'm trying to create a simple program.
If the user enters a number in the attacker ws field and the same number defender ws field you should get an answer.
I'm missing something.
package com.example.helloandroid;
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.TextView;
public class HelloAndroid extends Activity implements OnClickListener
{
private EditText text, text2, text3;
private Button btutorial1;
int result = text.toString().compareTo(text2.toString());
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("I hope this works");
text = (EditText) findViewById(R.id.editText1);
text2 =(EditText) findViewById(R.id.editText2);
text3 = (EditText) findViewById(R.id.editText3);
btutorial1 = (Button) findViewById(R.id.button1);
btutorial1.setOnClickListener(this);
}
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button1:
if (text.getText().toString().equals(1) && text2.getText().toString().equals(2))
{
text3.setText("Five and above");
return;
}
else if (text.getText().toString().equals(1) && text2.getText().toString().equals(3))
{
text3.setText("Five and above");
return;
}
else if (text.getText().toString().equals(1) && text2.getText().toString().equals(3))
{
text3.setText("Four and above");
return;
}
else if (text.getText().toString().equals(text2.getText().toString()))
{
text3.setText("Four and above");
return;
}
else
{
text3.setText("Not Working");
return;
}
}
}
}
My XML class named main
<?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:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Attacker ws"></TextView>
<EditText android:id="#+id/editText1" android:text="" android:layout_height="wrap_content" android:layout_width="match_parent" android:inputType="number" android:visibility="visible"></EditText>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Opponent ws"></TextView>
<EditText android:id="#+id/editText2" android:text="" android:layout_height="wrap_content" android:layout_width="match_parent" android:visibility="visible" android:inputType="number"></EditText>
<Button android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate" android:visibility="visible" android:onClick="myClickHandler"></Button>
<EditText android:id="#+id/editText3" android:layout_height="wrap_content" android:layout_width="match_parent" android:name="result" android:editable="false"></EditText>
</LinearLayout>
In java with objects == checks if 2 variables have the same memory location, not that they are the same value. Use .equals() instead and it should work
If you are comparing two integers then do this.
int number1 = Integer.parseInt(text);
int number2= Integer.parseInt(text2);
now in if condition compare number1 and number2.
if (number1 == number2)
{
text3.setText("Four and above");
return;
}
Remember this will work only for integers. for any string you will get exception you can handle it later on.
if you are planning to compare text then use use text.getText().toString().equals(text2.getText().toString())

Categories