我正在使用Android Studio,即使代码中没有错误,打开模拟器后我的应用也会崩溃。

MainActivity.java

package com.example.ss.flashcash;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class MainActivity extends ActionBarActivity {

    double money;
    double convert;
    EditText txtMoney;
    RadioButton radEuros, radPesos, radRupees;
    TextView txtResult;
    Button btnComputeCost;
    DecimalFormat hundredth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtMoney = (EditText) findViewById(R.id.txtMoney);
        radEuros = (RadioButton) findViewById(R.id.radEuros);
        radPesos = (RadioButton) findViewById(R.id.radPesos);
        radRupees = (RadioButton) findViewById(R.id.radRupees);
        btnComputeCost = (Button) findViewById(R.id.btnComputeCost);
        txtResult = (TextView) findViewById(R.id.txtResult);
        btnComputeCost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculate();
            }
        });
    }

    public void calculate()
    {
        hundredth = new DecimalFormat("#.##");
        money = Double.parseDouble(txtMoney.getText().toString());
        if (radEuros.isChecked())
        {
            if (money < 100000)
            {
                convert = money * .92;
                txtResult.setText("$" + hundredth.format(convert));
            }
            else Toast.makeText(this, "Enter less than 100,000", Toast.LENGTH_LONG).show();
        }
        if (radPesos.isChecked())
        {
            if (money < 100000)
            {
                convert = money * 15.02;
                txtResult.setText("$" + hundredth.format(convert));
            }
            else Toast.makeText(this, "Enter less than 100,000", Toast.LENGTH_LONG).show();
        }
        if (radRupees.isChecked())
        {
            if (money < 100000)
            {
                convert = money * 62.32;
                txtResult.setText("$" + hundredth.format(convert));
            }
            else Toast.makeText(this, "Enter less than 100,000", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Currency Converter"
        android:id="@+id/txtHeader"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp"
        android:typeface="sans"/>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:layout_centerVertical="true">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Euros"
            android:id="@+id/radEuros"
            android:layout_below="@+id/txtHeader"
            android:layout_alignLeft="@+id/txtHeader"
            android:layout_alignStart="@+id/txtHeader"
            android:layout_marginTop="129dp"
            android:checked="false"
            android:textColor="#FF0000"
            android:textSize="30dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pesos"
            android:id="@+id/radPesos"
            android:layout_below="@+id/radEuros"
            android:layout_alignLeft="@+id/radEuros"
            android:layout_alignStart="@+id/radEuros"
            android:checked="false"
            android:textColor="#19FF19"
            android:textSize="30dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rupees"
            android:id="@+id/radRupees"
            android:checked="false"
            android:layout_below="@+id/radPesos"
            android:layout_alignLeft="@+id/radPesos"
            android:layout_alignStart="@+id/radPesos"
            android:textColor="#FFFF19"
            android:textSize="30dp" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/txtMoney"
        android:layout_above="@+id/radioGroup"
        android:layout_centerHorizontal="true"
        android:hint="Enter $Amount"
        android:typeface="sans"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CONVERT"
        android:id="@+id/btnComputeCost"
        android:layout_marginTop="45dp"
        android:layout_below="@+id/radioGroup"
        android:layout_toLeftOf="@+id/txtHeader"
        android:layout_toStartOf="@+id/txtHeader" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/txtResult"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/btnComputeCost"
        android:layout_alignStart="@+id/btnComputeCost"
        android:typeface="sans"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/money3"
        android:layout_alignBottom="@+id/btnComputeCost"
        android:layout_toRightOf="@+id/radioGroup"
        android:layout_toEndOf="@+id/radioGroup" />

</RelativeLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ss.flashcash" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/moneypng"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:background="@color/blue">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


日志猫:

03-22 22:48:25.005    1929-1929/com.example.ss.flashcash I/art﹕ Not late-enabling -Xcheck:jni (already on)
03-22 22:48:29.458    1929-1941/com.example.ss.flashcash I/art﹕ Background sticky concurrent mark sweep GC freed 2956(243KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2045KB/2045KB, paused 1.594ms total 285.202ms
03-22 22:48:29.559    1929-1929/com.example.ss.flashcash D/AndroidRuntime﹕ Shutting down VM
03-22 22:48:29.559    1929-1929/com.example.ss.flashcash E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.ss.flashcash, PID: 1929
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ss.flashcash/com.example.ss.flashcash.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
            at com.example.ss.flashcash.MainActivity.onCreate(MainActivity.java:30)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


非常感谢您提供的所有帮助。

最佳答案

可能得到ClassCastException,因为这里:

    txtMoney = (EditText) findViewById(R.id.txtMoney);


txtMoneyTextView的ID,但尝试投射EditText

10-08 15:17