简体   繁体   English

应用程序崩溃,没有错误

[英]App Crashing, No Errors

I'm using Android Studio, and my app crashes as soon as I open the emulator, even though there are no errors in the code. 我正在使用Android Studio,即使代码中没有错误,打开模拟器后我的应用也会崩溃。

MainActivity.java 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 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 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>

logcat: 日志猫:

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)

Thanks so much for all of the help. 非常感谢您的所有帮助。

Probably getting ClassCastException because here : 大概得到ClassCastException因为这里:

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

txtMoney is id for TextView but trying to cast EditText txtMoneyTextView ID,但尝试投射EditText

Look at your layout file. 查看您的布局文件。 You have written: 你写:

...
<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"/>
...

Now inside your activity, your code: 现在在您的活动中,您的代码:

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

is raising ClassCastException because you are trying to cast a TextView to an EditText . 正在引发ClassCastException因为您试图将TextView转换为EditText

I hope you got the point. 我希望你明白了。

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

相关问题 我的应用不断崩溃,似乎找不到任何错误 - My app keeps crashing on me and cant seem to find any errors 为什么我的应用程序继续崩溃而没有任何错误? - Why does my app keep crashing without any errors? Tomcat 崩溃没有错误 - Tomcat is crashing without errors 点击时出现Android错误/崩溃 - Android errors/crashing on click Eclipse在启动时崩溃,没有错误 - Eclipse Crashing on Startup with No Errors 我的应用程序在启动时崩溃,我不知道为什么。 Gradle构建正常运行,没有任何错误 - My App is crashing on startup and i have no clue why. Gradle build runs fine without any errors 简单的 android 工作室应用程序在我运行时崩溃。 它没有错误 - Simple android studio app is crashing when i run it. it have no errors 我的聊天应用程序在 android 工作室运行时不断崩溃,但没有构建错误 - My chat app keeps crashing, on android studio when i run it but there no build errors 即使在检查调试和测试中的错误后,我的 android 应用程序仍会崩溃 - My android app keeps crashing even after checking for errors in debugging and tests 我正在 kotlin 中制作应用程序,但它一直在崩溃,但在启动之前它没有显示错误 - I am making app in kotlin but it keeps crashing, yet before launching it doesnt show errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM