Android - FATAL EXCEPTION: main - Unable to start activity ComponentInfo
Asked Answered
P

4

5

My problem is that when I run my app on the android emulator the app starts up but quickly crashes and displays the popup: "Unfortunately, TipCalculator has stopped." I have looked through the code and have searched the internet thoroughly for an answer. Yes I am a newby at android development. I have looked at the errors that have been generated by LogCat but do not know the problem and where to find it based on the errors I received.

Here is my 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
Protonema answered 4/11, 2013 at 8:5 Comment(3)
try to run application after clearing project from Project->CleanMellman
Could you try to clean and build the project, and relaunch the app ?Rittenhouse
I agree with @ρяσѕρєяK, I tried your code and it seems to be working fine.Constituent
M
4

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

at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.java:45)

LogCat is your friend.

You have a TextView in your layout, however in your code, you are trying to turn it into an EditText.

After re-reading your code, there does not appear to be a problem, please try Cleaning your project.

Project -> Clean

Mackinaw answered 4/11, 2013 at 8:12 Comment(2)
R.id.txtBill, R.id.txtTip, R.id.txtFinal all ids of editText onlyLegist
Please check line 45 in TipCalc.javaAnabaptist
C
3

From my experience, It's a problem with RelativeLayout. I don't know what's the problem, but following 2 solutions worked for me.

  1. Clean your project and run again. If it's not successful, then
  2. Change RelativeLayout to LinearLayout (This needs to remove relative attributes also. And a fair work to arrange views).
Chenee answered 4/11, 2013 at 8:22 Comment(3)
"Change RelativeLayout to LinearLayout". whats wrong with relative layout. no need for that. clean and build should workLegist
@Raghunandan: Yup,It should work. But I don't know, sometimes it doesn't.Chenee
@Chenee You are right... clean didn't work but changing from relative to linear fixed it..Crosson
P
0

You have probably a TextView in your layout and you use an EditText in your 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
Pipage answered 4/11, 2013 at 8:13 Comment(1)
R.id.txtBill, R.id.txtTip, R.id.txtFinal all ids of editText only .i too made a mistake posting it an answer before deleting itLegist
S
0

I had the same problem. It was fixed by running ant clean; ant debug.

This error occurred when I modified res/layout/main.xml and immediately do ant debug.

Sublapsarianism answered 28/1, 2014 at 3:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.