Mon problème est que lorsque je lance mon application sur l'émulateur Android, celle-ci démarre mais se bloque rapidement et affiche le message contextuel: "Malheureusement, TipCalculator s'est arrêté." J'ai parcouru le code et j'ai cherché Internet à fond pour une réponse. Oui, je suis nouveau au développement Android. J'ai examiné les erreurs générées par LogCat, mais je ne connais pas le problème et où le trouver en fonction des erreurs que j'ai reçues.
Voici mon code:
TipCalc.Java
package com.troysantry.tipcalculator;
import Android.os.Bundle;
import Android.app.Activity;
import Android.text.Editable;
import Android.text.TextWatcher;
import Android.view.Menu;
import Android.widget.EditText;
public class TipCalc extends Activity {
public static final String TOTAL_BILL = "TOTAL_BILL";
public static final String CURRENT_TIP = "CURRENT_TIP";
public static final String BILL_WITHOUT_TIP = "BILL_WITHOUT_TIP";
public double billBeforeTip;
public double tipAmount;
public double finalBill;
EditText txtBillBeforeTip;
EditText txtTipAmount;
EditText txtFinalBill;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip_calc);
if(savedInstanceState==null){
billBeforeTip=0.0;
tipAmount=0.15;
finalBill=0.0;
}
else{
billBeforeTip=savedInstanceState.getDouble(BILL_WITHOUT_TIP);
tipAmount=savedInstanceState.getDouble(CURRENT_TIP);
finalBill=savedInstanceState.getDouble(TOTAL_BILL);
}
txtBillBeforeTip = (EditText) findViewById(R.id.txtBill);
txtTipAmount=(EditText) findViewById(R.id.txtTip);
txtFinalBill=(EditText) findViewById(R.id.txtFinal);
txtBillBeforeTip.addTextChangedListener(billBeforeTipListener);
}
private TextWatcher billBeforeTipListener = new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
try{
billBeforeTip = Double.parseDouble(s.toString());
}
catch(NumberFormatException e){
billBeforeTip=0.0;
}
UpdateTipAndFinalBill();
}
};
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 void UpdateTipAndFinalBill(){
double tipAmount = Double.parseDouble(txtTipAmount.getText().toString());
double finalBill = billBeforeTip + (billBeforeTip*tipAmount);
txtFinalBill.setText(String.format("%.02f",finalBill));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tip_calc, menu);
return true;
}
}
activity_tip_calc.xml
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
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=".TipCalc" >
<TextView
Android:id="@+id/tvBill"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentLeft="true"
Android:layout_alignParentTop="true"
Android:layout_marginLeft="19dp"
Android:layout_marginTop="14dp"
Android:text="@string/bill_text_view" />
<TextView
Android:id="@+id/tvFinal"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignRight="@+id/txtBill"
Android:layout_below="@+id/txtBill"
Android:layout_marginRight="24dp"
Android:layout_marginTop="18dp"
Android:text="@string/final_text_view" />
<EditText
Android:id="@+id/txtFinal"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignBaseline="@+id/tvFinal"
Android:layout_alignBottom="@+id/tvFinal"
Android:layout_alignRight="@+id/txtTip"
Android:layout_marginRight="25dp"
Android:ems="6"
Android:inputType="numberDecimal"
Android:text="@string/final_bill_edit_text" />
<TextView
Android:id="@+id/tvTip"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_above="@+id/tvFinal"
Android:layout_centerHorizontal="true"
Android:text="@string/tip_text_view" />
<EditText
Android:id="@+id/txtTip"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignBaseline="@+id/tvTip"
Android:layout_alignBottom="@+id/tvTip"
Android:layout_toRightOf="@+id/tvTip"
Android:ems="5"
Android:inputType="numberDecimal"
Android:text="@string/tip_edit_text"
Android:textSize="16sp" />
<EditText
Android:id="@+id/txtBill"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignBaseline="@+id/tvBill"
Android:layout_alignBottom="@+id/tvBill"
Android:layout_toRightOf="@+id/tvBill"
Android:ems="5"
Android:inputType="numberDecimal"
Android:text="@string/bill_edit_text"
Android:textSize="16sp" />
</RelativeLayout>
LogCat
11-04 01:24:10.403: D/AndroidRuntime(1050): Shutting down VM
11-04 01:24:10.403: W/dalvikvm(1050): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 01:24:10.543: E/AndroidRuntime(1050): FATAL EXCEPTION: main
11-04 01:24:10.543: E/AndroidRuntime(1050): Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to Android.widget.EditText
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2211)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2261)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.ActivityThread.access$600(ActivityThread.Java:141)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1256)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.os.Handler.dispatchMessage(Handler.Java:99)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.os.Looper.loop(Looper.Java:137)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.ActivityThread.main(ActivityThread.Java:5103)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Java.lang.reflect.Method.invokeNative(Native Method)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Java.lang.reflect.Method.invoke(Method.Java:525)
11-04 01:24:10.543: E/AndroidRuntime(1050): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:737)
11-04 01:24:10.543: E/AndroidRuntime(1050): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
11-04 01:24:10.543: E/AndroidRuntime(1050): at dalvik.system.NativeStart.main(Native Method)
11-04 01:24:10.543: E/AndroidRuntime(1050): Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to Android.widget.EditText
11-04 01:24:10.543: E/AndroidRuntime(1050): at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.Java:45)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.Activity.performCreate(Activity.Java:5133)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1087)
11-04 01:24:10.543: E/AndroidRuntime(1050): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2175)
11-04 01:24:10.543: E/AndroidRuntime(1050): ... 11 more
11-04 01:29:11.177: I/Process(1050): Sending signal. PID: 1050 SIG: 9
11-04 01:31:32.080: D/AndroidRuntime(1109): Shutting down VM
11-04 01:31:32.080: W/dalvikvm(1109): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 01:31:32.194: E/AndroidRuntime(1109): FATAL EXCEPTION: main
11-04 01:31:32.194: E/AndroidRuntime(1109): Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to Android.widget.EditText
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2211)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2261)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.ActivityThread.access$600(ActivityThread.Java:141)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1256)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.os.Handler.dispatchMessage(Handler.Java:99)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.os.Looper.loop(Looper.Java:137)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.ActivityThread.main(ActivityThread.Java:5103)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Java.lang.reflect.Method.invokeNative(Native Method)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Java.lang.reflect.Method.invoke(Method.Java:525)
11-04 01:31:32.194: E/AndroidRuntime(1109): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:737)
11-04 01:31:32.194: E/AndroidRuntime(1109): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
11-04 01:31:32.194: E/AndroidRuntime(1109): at dalvik.system.NativeStart.main(Native Method)
11-04 01:31:32.194: E/AndroidRuntime(1109): Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to Android.widget.EditText
11-04 01:31:32.194: E/AndroidRuntime(1109): at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.Java:44)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.Activity.performCreate(Activity.Java:5133)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1087)
11-04 01:31:32.194: E/AndroidRuntime(1109): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2175)
11-04 01:31:32.194: E/AndroidRuntime(1109): ... 11 more
11-04 01:36:33.195: I/Process(1109): Sending signal. PID: 1109 SIG: 9
11-04 02:11:09.684: D/AndroidRuntime(1167): Shutting down VM
11-04 02:11:09.684: W/dalvikvm(1167): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 02:11:09.855: E/AndroidRuntime(1167): FATAL EXCEPTION: main
11-04 02:11:09.855: E/AndroidRuntime(1167): Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to Android.widget.EditText
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2211)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2261)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.ActivityThread.access$600(ActivityThread.Java:141)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1256)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.os.Handler.dispatchMessage(Handler.Java:99)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.os.Looper.loop(Looper.Java:137)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.ActivityThread.main(ActivityThread.Java:5103)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Java.lang.reflect.Method.invokeNative(Native Method)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Java.lang.reflect.Method.invoke(Method.Java:525)
11-04 02:11:09.855: E/AndroidRuntime(1167): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:737)
11-04 02:11:09.855: E/AndroidRuntime(1167): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
11-04 02:11:09.855: E/AndroidRuntime(1167): at dalvik.system.NativeStart.main(Native Method)
11-04 02:11:09.855: E/AndroidRuntime(1167): Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to Android.widget.EditText
11-04 02:11:09.855: E/AndroidRuntime(1167): at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.Java:44)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.Activity.performCreate(Activity.Java:5133)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1087)
11-04 02:11:09.855: E/AndroidRuntime(1167): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2175)
11-04 02:11:09.855: E/AndroidRuntime(1167): ... 11 more
Causée par: Java.lang.ClassCastException: Android.widget.TextView
ne peut pas être converti en Android.widget.EditText
à com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.Java:45)
LogCat est votre ami.
Vous avez un TextView dans votre mise en page, mais dans votre code, vous essayez de le transformer en un EditText.
Après avoir relu votre code, il ne semble pas y avoir de problème. Essayez de nettoyer votre projet.
Projet -> Nettoyer
D'après mon expérience, c'est un problème avec RelativeLayout
. Je ne sais pas quel est le problème, mais suivre 2 solutions a fonctionné pour moi.
RelativeLayout
en LinearLayout
(Cela doit également supprimer les attributs relatifs. Et un travail équitable pour organiser les vues).Vous avez probablement une TextView
dans votre disposition et vous utilisez une EditText
dans votre code.
11-04 01:24:10.543: E/AndroidRuntime(1050): Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to Android.widget.EditText
J'ai eu le même problème. Il a été corrigé en exécutant ant clean; ant debug
.
Cette erreur s'est produite lorsque j'ai modifié res/layout/main.xml
et que j'ai immédiatement ant debug
.