簡體   English   中英

沒有錯誤,但應用無法打開

[英]No errors but app doesn't open

我正在使用Android for Programms:應用程序驅動方法制作技巧計算器應用程序,我完全按照書中的代碼進行操作,並且沒有錯誤。 但是,當我嘗試運行該應用程序時,出現消息,“不幸的是,小費計算器已停止。”

誰能幫我找出為什么應用無法打開的原因?

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

該日志清楚地告訴您問題出在哪里:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

您試圖將TextView字段從布局文件轉換為Java EditText。

檢查activity_main.xml來查找mal-defined字段。

您將對象初始化為EditText

private EditText tipCustomEditText; //displays custom tip amount

但是隨后您發現一個(可能是)一個TextView的View並嘗試將其EditTextEditText

tipCustomEditText = (EditText)findViewById(R.id.customTipTextView);

將這些行更改為:

private TextView tipCustomEditText; //displays custom tip amount
tipCustomEditText = (TextView)findViewById(R.id.customTipTextView);

因此。

這里

tipCustomEditText = (EditText)findViewById(R.id.customTipTextView);

ID名稱為TextView但您的代碼嘗試轉換為EditText

無論如何,更確切地說,它在這里: MainActivity.java第65行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM