我正在关注Android for Programms:App Driven Approach来制作小费计算器应用程序,我完全按照书中的代码进行操作,并且没有错误。但是,当我尝试运行应用程序时,我收到消息,"不幸的是,提示计算器已停止。"
任何人都可以帮我找出应用程序无法打开的原因吗?
package com.example.tipcalculator;
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 MainActivity extends Activity {
private static final String BILL_TOTAL = "BILL_TOTAL";
private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
private double currentBillTotal; //bill amount entered by the user
private int currentCustomPercent; //tip% set with the seek bar
private EditText tip10EditText; //displays 10% tip
private EditText total10EditText; //displays total with 10% tip
private EditText tip15EditText; //displays 15% tip
private EditText total15EditText;//displays total with 15% tip
private EditText billEditText;
private EditText tip20EditText; //displays 20% tip
private EditText total20EditText; //displays total with 20% tip
private TextView customTipTextView; //dispalys custom tip percentage
private EditText tipCustomEditText; //displays custom tip amount
private EditText totalCustomEditText; //displays total with custom tip
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//check if app just started or is being restored from memory
if(savedInstanceState == null)//the app just started running
{
currentBillTotal = 0.0; //initialize the bill amount to zero
currentCustomPercent = 18; //initialize the custom tip to 18%
}//end if
else //app is being restored from memory, not execuuted from scratch
{
//initialize the bill amount to saved amount
currentBillTotal = savedInstanceState.getDouble(BILL_TOTAL);
//initialize the custom tip to saved amounth
currentCustomPercent = savedInstanceState.getInt(CUSTOM_PERCENT);
}
//get references to the 10%, 15%, 20% tip and total EditTexts
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);
//get textview displaying custom tip percentage
customTipTextView = (TextView)findViewById(R.id.customTipTextView);
//get teh custom tip and total EditTexts
tipCustomEditText = (EditText)findViewById(R.id.customTipTextView);
totalCustomEditText = (EditText)findViewById(R.id.totalCustomEditText);
//get the billEditText
billEditText = (EditText)findViewById(R.id.billEditText);
// billEditTextWwatcher handles billEditText's onTextChanged Event
billEditText.addTextChangedListener(billEditTextWatcher);
//get the Seekbar used to set the custom tip amount
SeekBar customSeekBar = (SeekBar)findViewById(R.id.customSeekBar);
customSeekBar.setOnSeekBarChangeListener(customSeekBarListener);
}
private void updateStandard()
{
//calculate bill total with a 10% tip
double tenPercentTip = currentBillTotal * .1;
double tenPercentTotal = currentBillTotal + tenPercentTip;
//set tipTenEditText's text to tenPercentTip
tip10EditText.setText(String.format("%.02f", tenPercentTip));
//set totalTenEditText's text to tenPercentTotal
total10EditText.setText(String.format("%.02f", tenPercentTotal));
//calculate bill total with a 15% tip
double fifteenPercentTip = currentBillTotal * .15;
double fifteenPercentTotal = currentBillTotal + fifteenPercentTip;
//set tip15EditText's text to fifteenPercentTip
tip15EditText.setText(String.format("%.02f", fifteenPercentTip));
//set total15EditText's text to fifteenPercentTotal
total15EditText.setText(String.format("%.02f", fifteenPercentTotal));
//calculate bill total with a 20% tip
double twentyPercentTip = currentBillTotal * .2;
double twentyPercentTotal = currentBillTotal + twentyPercentTip;
//set tip20EditText's text to 20PercentTip
tip20EditText.setText(String.format("%.02f", twentyPercentTip));
//set total20EditText's text to 20PercentTotal
total20EditText.setText(String.format("%.02f", twentyPercentTotal));
}
private void updateCustom()
{
//set customTipTextView's text to match the position of the SeekBar
customTipTextView.setText(currentCustomPercent + "%");
//calculate the custom tip amount
double customTipAmount = currentBillTotal * currentCustomPercent * .01;
//calculate total bill, including custom tip
double customTotalAmount = currentBillTotal + customTipAmount;
//display the tip and total bill amounts
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);
}
//called when the user changes position of SeekBar
private OnSeekBarChangeListener customSeekBarListener = new OnSeekBarChangeListener()
{
//update currentCustomPercent, then call updateCustom
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//sets currentcustompercent to position of the SeekBar's thumb
currentCustomPercent = seekBar.getProgress();
updateCustom(); //update EditTexts for custom tip and total
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
//end method onStartTrckingTouch
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
//end method onStopTracking Touch
}
};
//event-handling object that responds to billEditText's events
private TextWatcher billEditTextWatcher = new TextWatcher()
{
//called when the user enters a number
@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)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
};
}
这是日志猫:
07-06 13:10:34.590: W/asset(14247): Copying FileAsset 0x727a6928 (zip:/data/app/com.example.tipcalculator- 1.apk:/resources.arsc) to buffer size 95268 to make it aligned.
07-06 13:10:34.670: W/dalvikvm(14247): threadid=1: thread exiting with uncaught exception (group=0x416ebe18)
07-06 13:10:34.680: E/AndroidRuntime(14247): FATAL EXCEPTION: main
07-06 13:10:34.680: E/AndroidRuntime(14247): Process: com.example.tipcalculator, PID: 14247
07-06 13:10:34.680: E/AndroidRuntime(14247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tipcalculator/com.example.tipcalculator.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.ActivityThread.access$800(ActivityThread.java:156)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.os.Handler.dispatchMessage(Handler.java:102)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.os.Looper.loop(Looper.java:157)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.ActivityThread.main(ActivityThread.java:5872)
07-06 13:10:34.680: E/AndroidRuntime(14247): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 13:10:34.680: E/AndroidRuntime(14247): at java.lang.reflect.Method.invoke(Method.java:515)
07-06 13:10:34.680: E/AndroidRuntime(14247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
07-06 13:10:34.680: E/AndroidRuntime(14247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
07-06 13:10:34.680: E/AndroidRuntime(14247): at dalvik.system.NativeStart.main(Native Method)
07-06 13:10:34.680: E/AndroidRuntime(14247): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-06 13:10:34.680: E/AndroidRuntime(14247): at com.example.tipcalculator.MainActivity.onCreate(MainActivity.java:65)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.Activity.performCreate(Activity.java:5312)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
07-06 13:10:34.680: E/AndroidRuntime(14247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
07-06 13:10:34.680: E/AndroidRuntime(14247): ... 11 more
07-06 13:10:34.690: D/Process(14247): killProcess, pid=14247
07-06 13:10:34.690: D/Process(14247): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
07-06 13:10:50.410: W/asset(14400): Copying FileAsset 0x72751068 (zip:/data/app/com.example.tipcalculator-1.apk:/resources.arsc) to buffer size 95268 to make it aligned.
07-06 13:10:50.520: W/dalvikvm(14400): threadid=1: thread exiting with uncaught exception (group=0x416ebe18)
07-06 13:10:50.530: E/AndroidRuntime(14400): FATAL EXCEPTION: main
07-06 13:10:50.530: E/AndroidRuntime(14400): Process: com.example.tipcalculator, PID: 14400
07-06 13:10:50.530: E/AndroidRuntime(14400): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tipcalculator/com.example.tipcalculator.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.ActivityThread.access$800(ActivityThread.java:156)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.os.Handler.dispatchMessage(Handler.java:102)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.os.Looper.loop(Looper.java:157)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.ActivityThread.main(ActivityThread.java:5872)
07-06 13:10:50.530: E/AndroidRuntime(14400): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 13:10:50.530: E/AndroidRuntime(14400): at java.lang.reflect.Method.invoke(Method.java:515)
07-06 13:10:50.530: E/AndroidRuntime(14400): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
07-06 13:10:50.530: E/AndroidRuntime(14400): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
07-06 13:10:50.530: E/AndroidRuntime(14400): at dalvik.system.NativeStart.main(Native Method)
07-06 13:10:50.530: E/AndroidRuntime(14400): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-06 13:10:50.530: E/AndroidRuntime(14400): at com.example.tipcalculator.MainActivity.onCreate(MainActivity.java:65)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.Activity.performCreate(Activity.java:5312)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
07-06 13:10:50.530: E/AndroidRuntime(14400): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
07-06 13:10:50.530: E/AndroidRuntime(14400): ... 11 more
答案 0 :(得分:1)
日志清楚地告诉您问题所在:
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
您正在尝试将TextView字段从布局文件转换为Java EditText。
检查activity_main.xml以查找错误定义的字段。
答案 1 :(得分:1)
您将对象初始化为EditText
private EditText tipCustomEditText; //displays custom tip amount
但是你找到一个(可能)是TextView
的视图并尝试将其转换为EditText
tipCustomEditText = (EditText)findViewById(R.id.customTipTextView);
将这些行更改为:
private TextView tipCustomEditText; //displays custom tip amount
tipCustomEditText = (TextView)findViewById(R.id.customTipTextView);
因此
答案 2 :(得分:0)
下面
tipCustomEditText = (EditText)findViewById(R.id.customTipTextView);
ID名称显示为TextView
,但您的代码会尝试转换为EditText
。
无论如何更具体,它在这里:MainActivity.java
第65行