- Forum posts: 1
May 19, 2017, 9:30:35 AM via Website
May 19, 2017 9:30:35 AM via Website
Hi
I am new to android development. I am following a book from Deitel namely 'Android for programmer , An app Driven approach' . I am working on chapter 4 'Tip Calculator application'. I am almost done typing and removed most of the compilation errors but I am not able to resolve one compilation error.
'Error incompatibel types, integer cannot be converted to bundle'
This error shows on onCreate function of Activity class.
super.onCreate(R.layout.main); R.layout.main is underlined red.
The code for Activity class and main.xml is given below.
**Activity Class code**
package biz.itwise.tipcalculatoe_v2;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class tipCalculator extends Activity {
private static final String BILL_TOTAL = "BILL_TOTAL";
private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
private double currentBillTotal;
private int currentCustomPercent;
private EditText tip10EditText;
private EditText total10EditText;
private EditText tip15EditText;
private EditText total15EditText;
private EditText billEditText;
private EditText tip20EditText;
private EditText total20EditText;
private TextView customTipTextView;
private EditText tipCustomEditText;
private EditText totalCustomEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(R.layout.main);
if (savedInstanceState == null) {
currentBillTotal = 0.0;
currentCustomPercent = 18;
}
else {
currentBillTotal = savedInstanceState.getDouble(BILL_TOTAL);
currentCustomPercent = savedInstanceState.getInt(CUSTOM_PERCENT);
}
tip10EditText = (EditText) findViewById(R.id.tip10EditText);
total10EditText = (EditText) findViewById(R.id.total10EditText);
tip15EditText = (EditText) findViewById(R.id.tip15EditText);
total15EditText = (EditText) findViewById(R.id.total15EditText);
tip20EditText = (EditText) findViewById(R.id.tip20EditText);
total20EditText = (EditText) findViewById(R.id.total20EditText);
customTipTextView = (TextView) findViewById(R.id.customTipTextView);
tipCustomEditText = (EditText) findViewById(R.id.tipCustomEditText);
totalCustomEditText = (EditText) findViewById(R.id.totalCustomEditText);
billEditText = (EditText) findViewById(R.id.billEditText);
billEditText.addTextChangedListener(billEditTextWatcher);
SeekBar customSeekBar = (SeekBar) findViewById(R.id.customseekBar);
customSeekBar.setOnSeekBarChangeListener(customSeekBarListener);
}
private void updateStandard() {
double tenPercentTip = currentBillTotal * .1;
double tenPercentTotal = currentBillTotal + tenPercentTip;
tip10EditText.setText(String.format("%.02f", tenPercentTip));
total10EditText.setText(String.format("%.02f", tenPercentTotal));
double fifteenPercentTip = currentBillTotal * .15;
double fifteenPercentTotal = currentBillTotal + fifteenPercentTip;
tip15EditText.setText(String.format("%.02f", fifteenPercentTip));
total15EditText.setText(String.format("%.02f", fifteenPercentTotal));
double twentyPercentTip = currentBillTotal * .20;
double twentyPercentTotal = currentBillTotal + twentyPercentTip;
tip20EditText.setText(String.format("%.02f", twentyPercentTip));
total20EditText.setText(String.format("%.02f", twentyPercentTotal));
}
private void updateCustom() {
customTipTextView.setText(currentCustomPercent + "%");
double customTipAmount = currentBillTotal * currentCustomPercent * .01;
double customTotalAmount = currentBillTotal + customTipAmount;
tipCustomEditText.setText(String.format("%.02f", customTipAmount));
totalCustomEditText.setText(String.format("%.02f", customTotalAmount));
}
@Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putDouble(BILL_TOTAL,currentBillTotal);
outState.putInt( CUSTOM_PERCENT, currentCustomPercent );
}
private OnSeekBarChangeListener customSeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
currentCustomPercent = seekBar.getProgress();
updateCustom();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
private TextWatcher billEditTextWatcher = new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
currentBillTotal = Double.parseDouble(s.toString());
}
catch (NumberFormatException e) {
currentBillTotal = 0.0;
updateStandard();
updateCustom();
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
Main.XML code
<EditText
android:id="@+id/total10EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/zero"
android:focusable="false"
android:longClickable="false"
android:cursorVisible="false"/>
<EditText
android:id="@+id/total15EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/zero"
android:focusable="false"
android:longClickable="false"
android:cursorVisible="false"/>
<EditText
android:id="@+id/total20EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cursorVisible="false"
android:focusable="false"
android:gravity="center"
android:longClickable="false"
android:text="@string/zero" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/customTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right|center_vertical"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:text="@string/custom"
android:textColor="#000"
android:focusable="false"/>
<SeekBar
android:id="@+id/customseekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingLeft="8dp"
android:progress="18" />
<TextView
android:id="@+id/customTipTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:focusable="false"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:text="18%"
android:textColor="#000" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tipCustomTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="@string/tip"
android:textColor="#000" />
<EditText
android:id="@+id/tipCustomEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cursorVisible="false"
android:focusable="false"
android:gravity="center"
android:longClickable="false"
android:text="@string/zero"
android:textSize="14sp" />
<TextView
android:id="@+id/totalCustomTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right|center"
android:paddingRight="5dp"
android:text="@string/total"
android:textColor="#000" />
<EditText
android:id="@+id/totalCustomEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cursorVisible="false"
android:focusable="false"
android:gravity="center"
android:longClickable="false"
android:text="@string/zero"
android:textSize="14sp" />
</TableRow>
Grateful if anybody can help me.