Reputation: 1
I'm a novice in the programming field and new help. In my xml file I have defined radio buttons that when selected a specific computation can be made but anytime it's checked, the app force closes. I also want to do a computation when a button is clicked, but when clicked, the app force closes. Please I need your expertise to be able to submit my assignment. Thanks in advance!!
Below is the MainActivity.java file
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
implements android.view.View.OnClickListener{
Button compute;
EditText inputRate, inputAmount, inputAns;
RadioButton weekly, monthly, yearly;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputRate = (EditText)findViewById(R.id.inputRate);
inputAmount = (EditText)findViewById(R.id.inputAmount);
inputAns = (EditText)findViewById(R.id.inputAns);
inputAns.setEnabled(false);
compute = (Button)findViewById(R.id.buttoncompute);
weekly = (RadioButton)findViewById(R.id.radioWeekly);
monthly = (RadioButton)findViewById(R.id.radioMonthly);
yearly = (RadioButton)findViewById(R.id.radioYearly);
compute.setOnClickListener(this);
weekly.setOnClickListener(this);
monthly.setOnClickListener(this);
yearly.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
Toast toast = Toast(compute);
toast.show();
double inputRateValue = -1;
try {inputRateValue = Double.parseDouble(inputRate.getText() .toString());
// TODO Auto-generated method stub
}
catch (NumberFormatException e)
{
}
double inputAmountValue = -1;
try {
inputAmountValue = Double.parseDouble(inputAmount.getText() .toString());
}
catch (NumberFormatException e)
{
}
double inputAns = 0;
inputAns = inputRateValue * inputAmountValue;
inputAns += Double.parseDouble(compute.getText() .toString());
compute.setText("" + inputAns);
}
private Toast Toast(Button compute2) {
// TODO Auto-generated method stub
return null;
}
}
Below is my xml file
<TextView
android:id="@+id/interestRate"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:text="@string/interest_rate"
/>
<EditText
android:id="@+id/inputRate"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/interestRate"
android:layout_alignTop="@+id/interestRate"
android:layout_toRightOf="@+id/interestRate"
android:ems="10"
android:inputType="number" />
<RadioButton
android:id="@+id/radioWeekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/interestRate"
android:layout_below="@+id/interestRate"
android:textColor="#FFFFFF"
android:layout_marginTop="19dp"
android:text="@string/weekly" />
<RadioButton
android:id="@+id/radioMonthly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioWeekly"
android:layout_below="@+id/radioWeekly"
android:textColor="#FFFFFF"
android:layout_marginTop="19dp"
android:text="@string/monthly" />
<RadioButton
android:id="@+id/radioYearly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioMonthly"
android:layout_below="@+id/radioMonthly"
android:textColor="#FFFFFF"
android:layout_marginTop="19dp"
android:text="@string/yearly" />
<TextView
android:id="@+id/textAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioYearly"
android:textColor="#FFFFFF"
android:layout_marginTop="19dp"
android:textSize="22sp"
android:text="@string/amount" />
<EditText
android:id="@+id/inputAmount"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textAmount"
android:layout_alignBottom="@+id/textAmount"
android:layout_toRightOf="@+id/interestRate"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/buttoncompute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textAmount"
android:layout_alignRight="@+id/interestRate"
android:layout_below="@+id/inputAmount"
android:layout_marginTop="27dp"
android:onClick=""
android:text="@string/compute" />
<TextView
android:id="@+id/textResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttoncompute"
android:layout_marginTop="22dp"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:text="@string/result" />
<EditText
android:id="@+id/inputAns"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/inputRate"
android:layout_alignTop="@+id/textResult"
android:ems="10"
android:inputType="number" />
Upvotes: 0
Views: 95
Reputation: 15
you need to surround the radio buttons with radio group like this xml file below :
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="34dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
and defining the radio button in java like this :
Radiogroup group;
group.add(radiobutton1);
for yours Toast toast = Toast(compute);toast.show();
it should be like this:
String s ;
if(radiobutton1.isChecked){
s = "weekly";
Toast toast = Toast(this,s,Toast.LENGTH_SHORT);toast.show();}
where is s
a String
and inside it the sentence you want and this
mean the context i think or the class and the last Toast.LENGTH_SHORT
is the time of the toast.
if it help accept if not post the log can thank you.
Upvotes: 0
Reputation: 22018
Toast toast = Toast(compute);
where compute
is a Button
does not make sense. Look for a tutorial on how to show a Toast
, there's hundreds out there and it's easy. See this question / answer for example
For the future: you have to post your logcat output for people to see what the problem is.
Upvotes: 1