简体   繁体   English

Android - FATAL EXCEPTION:main - 无法启动活动ComponentInfo

[英]Android - FATAL EXCEPTION: main - Unable to start activity ComponentInfo

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." 我的问题是,当我在Android模拟器上运行我的应用程序时,应用程序启动但很快崩溃并显示弹出窗口:“不幸的是,TipCalculator已停止。” I have looked through the code and have searched the internet thoroughly for an answer. 我查看了代码并彻底搜索了互联网以获得答案。 Yes I am a newby at android development. 是的我是android开发的新手。 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. 我查看了LogCat生成的错误,但不知道问题以及根据收到的错误在哪里找到它。

Here is my code: 这是我的代码:

TipCalc.java TipCalc.java

package com.troysantry.tipcalculator; 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 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 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

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 引起: java.lang.ClassCastException: android.widget.TextView无法强制转换为android.widget.EditText

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

LogCat is your friend. LogCat是你的朋友。

You have a TextView in your layout, however in your code, you are trying to turn it into an EditText. 您的布局中有TextView,但是在您的代码中,您尝试将其转换为EditText。

After re-reading your code, there does not appear to be a problem, please try Cleaning your project. 重新阅读您的代码后,似乎没有问题,请尝试清理您的项目。

Project -> Clean 项目 - >清洁

From my experience, It's a problem with RelativeLayout . 根据我的经验,这是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). RelativeLayout更改为LinearLayout (这也需要删除相关属性。以及安排视图的公平工作)。

您的布局中可能有TextViewEditText在代码中使用EditText

11-04 01:24:10.543: E/AndroidRuntime(1050): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

I had the same problem. 我有同样的问题。 It was fixed by running ant clean; ant debug 它是通过运行ant clean; ant debug修复的ant clean; ant debug ant clean; ant debug . ant clean; ant debug

This error occurred when I modified res/layout/main.xml and immediately do ant debug . 修改res/layout/main.xml并立即执行ant debug时发生此错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - FATAL EXCEPTION:main - 无法启动活动ComponentInfo NullPointerException - Android - FATAL EXCEPTION: main - Unable to start activity ComponentInfo NullPointerException FATAL异常无法启动活动ComponentInfo - FATAL Exception Unable to start activity ComponentInfo 致命异常:主要无法启动活动android - Fatal Exception: main unable to start activity android 致命异常:main java.lang.RuntimeException:无法启动活动ComponentInfo - FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo AndroidRuntime:致命异常:main java.lang.RuntimeException:无法启动活动ComponentInfo - AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo E / AndroidRuntime:FATAL EXCEPTION:主PID:19302 java.lang.RuntimeException:无法启动活动ComponentInfo - E/AndroidRuntime: FATAL EXCEPTION: main PID: 19302 java.lang.RuntimeException: Unable to start activity ComponentInfo 致命异常:主要无法启动活动ComponentInfo原因:java.lang.NullPointerException - FATAL EXCEPTION: main Unable to start activity ComponentInfo Caused by: java.lang.NullPointerException 致命异常:主要无法启动活动ComponentInfo由java.lang.NullPointerException引起 - FATAL EXCEPTION: main Unable to start activity ComponentInfo Caused by java.lang.NullPointerException 无法启动活动ComponentInfo | E / AndroidRuntime:致命异常 - Unable to start activity ComponentInfo | E/AndroidRuntime: FATAL EXCEPTION 严重异常:无法启动活动ComponentInfo {…}:java.lang.NullPointerException - FATAL EXCEPTION: Unable to start activity ComponentInfo{…}: java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM