Application Failure Detected in android



I am new android development. so I saw video and created this small TIP CALCULATOR. But when I run my app it closes and gives error Application Failure Detected Try Again. I really want to know where I go wrong in these codes.


I have added two files which are:


CrazyTipCalc.java



package com.ashish.crazytipcalc;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class CrazyTipCalc extends Activity {

private static final String TOTAL_BILL = "TOTAL_BILL";
private static final String CURRENT_TIP = "CURRENT_TIP";
private static final String BILL_WITHOUT_TIP = "BILL_WITHOUT_TIP";

private double billBeforeTip, tipAmount, finalBill;

EditText billBeforeTipET, tipAmountET,finalBillET;

SeekBar tipSeekBar;

private int[] checkListValue = new int[12];

CheckBox friendlyCB, spacialsCB, opinionCB;

RadioGroup radioGroup1;

RadioButton badRadio, goodRadio, bestRadio;

Spinner problemSpinner;

Button startButton, pauseButton, resetButton;

Chronometer timeWaitChronometer;

long secondsYouWaited = 0;

TextView timeWatingTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crazy_tip_calc);

if(savedInstanceState == null){
billBeforeTip = 0.0;
tipAmount = .15;
finalBill = 0.0;
}else {
billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
tipAmount = savedInstanceState.getDouble(CURRENT_TIP);
finalBill = savedInstanceState.getDouble(TOTAL_BILL);
}

billBeforeTipET = (EditText) findViewById(R.id.billEditText);
tipAmountET = (EditText) findViewById(R.id.tipEditText);
finalBillET = (EditText) findViewById(R.id.finalEditText);

tipSeekBar = (SeekBar) findViewById(R.id.changeTipSeekBar);
tipSeekBar.setOnSeekBarChangeListener(tipSeekBarListener);

billBeforeTipET.addTextChangedListener(billBeforeTipListener);

friendlyCB = (CheckBox) findViewById(R.id.friendlyCheckBox);
spacialsCB = (CheckBox) findViewById(R.id.spacialCheckBox);
opinionCB = (CheckBox) findViewById(R.id.opinionCheckBox);

setUpIntroCheckBoxes();

radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
badRadio = (RadioButton) findViewById(R.id.badRadio);
goodRadio = (RadioButton) findViewById(R.id.goodRadio);
bestRadio = (RadioButton) findViewById(R.id.bestRadio);

addChangeListenerToRadio();

problemSpinner = (Spinner) findViewById(R.id.problemSpinner);
addItemSelectedListeners();

startButton = (Button) findViewById(R.id.startButtonTextView);
pauseButton = (Button) findViewById(R.id.pauseButton);
resetButton = (Button) findViewById(R.id.resetButton);

setButtonOnClickListener();

timeWaitChronometer = (Chronometer) findViewById(R.id.timeWaitChronometer);
timeWatingTextView = (Chronometer) findViewById(R.id.timeWaitTextView);

}

private TextWatcher billBeforeTipListener = new TextWatcher () {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try{
billBeforeTip = Double.parseDouble(s.toString());
}
catch(NumberFormatException e){
updateTipAndFinalBill();
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
//TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
//TODO Auto-generated method stub

}
};

private void updateTipAndFinalBill(){
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
double finalBill = billBeforeTip + (billBeforeTip * tipAmount);
finalBillET.setText(String.format("%.02f", finalBill));
}

protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(TOTAL_BILL, finalBill);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
}

private OnSeekBarChangeListener tipSeekBarListener = new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
tipAmount = (tipSeekBar.getProgress()) * .01;
tipAmountET.setText(String.format("%.02f", tipAmount));
updateTipAndFinalBill();

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//TODO Auto-generated method stub

}

};

private void setUpIntroCheckBoxes(){
friendlyCB.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
checkListValue[0] = (friendlyCB.isChecked())?4:0;
setTipFromWaitressChecklist();
updateTipAndFinalBill();
}
});

opinionCB.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
checkListValue[2] = (opinionCB.isChecked())?2:0;
setTipFromWaitressChecklist();
updateTipAndFinalBill();
}
});

spacialsCB.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
checkListValue[1] = (spacialsCB.isChecked())?1:0;
setTipFromWaitressChecklist();
updateTipAndFinalBill();
}
});
}

private void setTipFromWaitressChecklist(){
int ChecklistTotal = 0;
for(int item : checkListValue){
ChecklistTotal += item;
}
tipAmountET.setText(String.format("%.02f", ChecklistTotal * 0.1));
}

private void addChangeListenerToRadio(){
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

checkListValue[2] = (badRadio.isChecked())?-1:0;
checkListValue[3] = (goodRadio.isChecked())?2:0;
checkListValue[4] = (bestRadio.isChecked())?4:0;
setTipFromWaitressChecklist();
updateTipAndFinalBill();
}
});
}

private void addItemSelectedListeners(){
problemSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkListValue[6] = (problemSpinner.getSelectedItem()).equals("bad")?-1:0;
checkListValue[7] = (problemSpinner.getSelectedItem()).equals("good")?3:0;
checkListValue[8] = (problemSpinner.getSelectedItem()).equals("best")?6:0;
setTipFromWaitressChecklist();
updateTipAndFinalBill();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}

private void setButtonOnClickListener(){
startButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int stoppedMilliseconds = 0;
String chronoText = timeWaitChronometer.getText().toString();
String array[] = chronoText.split(":");

if(array.length == 2){
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000 +
Integer.parseInt(array[1]) * 1000;
}else if(array.length == 3){
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000 +
Integer.parseInt(array[1]) * 60 * 1000 +
Integer.parseInt(array[2]) * 1000;
}
timeWaitChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
secondsYouWaited = Long.parseLong(array[1]);
updateTipBasedOnTimeWaited(secondsYouWaited);
timeWaitChronometer.start();
}
});

pauseButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
timeWaitChronometer.stop();

}

});

resetButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
timeWaitChronometer.setBase(SystemClock.elapsedRealtime());
secondsYouWaited = 0;

}
});
}

private void updateTipBasedOnTimeWaited(long secondsYouWaited) {
checkListValue[9] = (secondsYouWaited > 10)?-2:2;
setTipFromWaitressChecklist();
updateTipAndFinalBill();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.crazy_tip_calc, menu);
return true;
}

}


and


Activity_Crazy_Tip_Calc.XML



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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=".CrazyTipCalc" >

<TextView
android:id="@+id/billTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="15dp"
android:text="@string/bill_text_view" />

<EditText
android:id="@+id/billEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/billTextView"
android:layout_alignBottom="@+id/billTextView"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/billTextView"
android:ems="5"
android:inputType="numberDecimal"
android:text="@string/bill_edit_text" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/billTextView"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/billEditText"
android:text="@string/tip_text_view" />

<EditText
android:id="@+id/tipEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tipTextView"
android:layout_alignBottom="@+id/tipTextView"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/tipTextView"
android:ems="4"
android:inputType="numberDecimal"
android:text="@string/tip_edit_text" />

<SeekBar
android:id="@+id/changeTipSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/changeTipTextView"
android:layout_marginLeft="27dp"
android:layout_toRightOf="@+id/changeTipTextView"
android:progress="15" />

<TextView
android:id="@+id/finalTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/changeTipSeekBar"
android:layout_marginTop="14dp"
android:layout_toLeftOf="@+id/changeTipSeekBar"
android:text="@string/final_text_view" />

<EditText
android:id="@+id/finalEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/finalTextView"
android:layout_alignBottom="@+id/finalTextView"
android:layout_alignRight="@+id/changeTipSeekBar"
android:ems="10"
android:inputType="numberDecimal"
android:text="@string/final_edit_text" />

<TextView
android:id="@+id/changeTipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/billTextView"
android:layout_below="@+id/billEditText"
android:layout_marginTop="30dp"
android:text="@string/change_tip_text_view" />

<TextView
android:id="@+id/introTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/finalTextView"
android:layout_below="@+id/finalEditText"
android:layout_marginTop="15dp"
android:text="@string/intro_text_view" />

<CheckBox
android:id="@+id/opinionCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/spacialCheckBox"
android:layout_alignBottom="@+id/spacialCheckBox"
android:layout_alignParentRight="true"
android:text="@string/opinion_text_view" />

<CheckBox
android:id="@+id/friendlyCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/introTextView"
android:layout_below="@+id/introTextView"
android:text="@string/friendly_text_view" />

<CheckBox
android:id="@+id/spacialCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/friendlyCheckBox"
android:layout_alignBottom="@+id/friendlyCheckBox"
android:layout_centerHorizontal="true"
android:text="@string/spacial_text_view" />

<TextView
android:id="@+id/availTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/introTextView"
android:layout_below="@+id/friendlyCheckBox"
android:text="@string/available_text_view" />

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/friendlyCheckBox"
android:layout_below="@+id/availTextView"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/badRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/bad_radiobutton" />
</RadioGroup>

<RadioButton
android:id="@+id/goodRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/availTextView"
android:layout_toRightOf="@+id/friendlyCheckBox"
android:text="@string/good_radiobutton" />

<Spinner
android:id="@+id/problemSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/bestRadio"
android:layout_marginTop="27dp"
android:entries="@array/problem_solving" />

<TextView
android:id="@+id/timeWaitTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/goodRadio"
android:layout_below="@+id/problemSpinner"
android:text="@string/time_waiting_text_view" />

<Chronometer
android:id="@+id/timeWaitChronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bestRadio"
android:layout_alignTop="@+id/timeWaitTextView"
android:layout_toLeftOf="@+id/opinionCheckBox"
android:text="@string/chronometer_view" />

<Button
android:id="@+id/startButtonTextView"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/availTextView"
android:layout_below="@+id/timeWaitChronometer"
android:layout_marginTop="22dp"
android:text="@string/start_button" />

<Button
android:id="@+id/resetButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/pauseButton"
android:layout_alignBottom="@+id/pauseButton"
android:layout_toRightOf="@+id/spacialCheckBox"
android:text="@string/reset_button" />

<Button
android:id="@+id/pauseButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/startButtonTextView"
android:layout_alignBottom="@+id/startButtonTextView"
android:layout_alignLeft="@+id/spacialCheckBox"
android:text="@string/pause_button" />

<RadioButton
android:id="@+id/bestRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/resetButton"
android:layout_below="@+id/availTextView"
android:text="@string/best_radiobutton" />

</RelativeLayout>

No comments:

Post a Comment