簡體   English   中英

致命異常主要

[英]Fatal exception main

我正在構建一個可轉換不同單位的應用程序,並且收到“致命異常主消息”消息,但是我的代碼一直運行到啟動該應用程序並顯示四個按鈕時,但是當您單擊其中一個按鈕時,它就會崩潰。 我沒有其他錯誤。

主要活動:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.lacrym0sadevelopment.convertix";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

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

public void unitLength(View view) {
    Intent intent = new Intent(this, CalculateLength.class);
    EditText editText = (EditText) findViewById(R.id.button1);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}// unitLength

public void unitWeight(View view) {
    Intent intent = new Intent(this, CalculateWeight.class);
    startActivity(intent);
}// unitWeigth

public void unitVolume(View view) {
    Intent intent = new Intent(this, CalculateVolume.class);
    startActivity(intent);
}// unitVolume

public void unitSpeed(View view) {
    Intent intent = new Intent(this, CalculateSpeed.class);
    startActivity(intent);
}// unitSpeed



}// MainActivity

CalculateLength:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class CalculateLength extends Activity {

private EditText text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (EditText) findViewById(R.id.editText1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void calculate(View view) {

    RadioButton feetButton = (RadioButton) findViewById(R.id.radio0);
    RadioButton metreButton = (RadioButton) findViewById(R.id.radio1);
    RadioButton kmButton = (RadioButton) findViewById(R.id.radio2);
    RadioButton milesButton = (RadioButton) findViewById(R.id.radio3);

    if (text.getText().length() == 0) {

        Toast.makeText(this, "Enter a Value", Toast.LENGTH_LONG).show();
    } else {

        double inputValue = Double.parseDouble(text.getText().toString());

        if (feetButton.isChecked()) {
            text.setText(String.valueOf(convertToFeet(inputValue)));

            feetButton.setChecked(true);

            metreButton.setChecked(false);

            kmButton.setChecked(false);

            milesButton.setChecked(false);

        }
        if (metreButton.isChecked()) {
            text.setText(String.valueOf(convertToMetre(inputValue)));

            feetButton.setChecked(false);

            metreButton.setChecked(true);

            kmButton.setChecked(false);

            milesButton.setChecked(false);

        }
        if (kmButton.isChecked()) {
            text.setText(String.valueOf(convertToKm(inputValue)));

            feetButton.setChecked(false);

            metreButton.setChecked(false);

            kmButton.setChecked(true);

            milesButton.setChecked(false);

        }
        if (milesButton.isChecked()) {
            text.setText(String.valueOf(convertToMiles(inputValue)));

            feetButton.setChecked(false);

            metreButton.setChecked(false);

            kmButton.setChecked(false);

            milesButton.setChecked(true);

        }
    }

}

private double convertToFeet(double inputValue) {

    return (inputValue * 3.2808);
}

private double convertToMetre(double inputValue) {

    return (inputValue / 3.2808);
}

private double convertToKm(double inputValue) {

    return (inputValue / 0.62137);
}

private double convertToMiles(double inputValue) {

    return (inputValue * 0.62137);
}

}//CalculateLength

Logcat輸出:

01-10 04:58:34.599: E/AndroidRuntime(1155): FATAL EXCEPTION: main
01-10 04:58:34.599: E/AndroidRuntime(1155): Process: com.lacrym0sadevelopment.convertix, PID: 1155
01-10 04:58:34.599: E/AndroidRuntime(1155): java.lang.IllegalStateException: Could not execute method of the activity
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.view.View$1.onClick(View.java:3814)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.view.View.performClick(View.java:4424)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.view.View$PerformClick.run(View.java:18383)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.os.Handler.handleCallback(Handler.java:733)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.os.Looper.loop(Looper.java:137)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invoke(Method.java:515)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at dalvik.system.NativeStart.main(Native Method)
01-10 04:58:34.599: E/AndroidRuntime(1155): Caused by: java.lang.reflect.InvocationTargetException
01-10 04:58:34.599: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invoke(Method.java:515)
01-10 04:58:34.599: E/AndroidRuntime(1155):     at android.view.View$1.onClick(View.java:3809)
01-10 04:58:34.599: E/AndroidRuntime(1155):     ... 11 more
01-10 04:58:34.599: E/AndroidRuntime(1155): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
01-10 04:58:34.599: E/AndroidRuntime(1155):     at com.lacrym0sadevelopment.convertix.MainActivity.unitLength(MainActivity.java:31)
01-10 04:58:34.599: E/AndroidRuntime(1155):     ... 14 more

似乎您正在嘗試將Button投射到此處的EditText

EditText editText = (EditText) findViewById(R.id.button1);

什么導致

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM