bork bork - 1 month ago 15
Android Question

Fatal exception main

I am building an app which convert different units and I'm getting the "Fatal exception main" message, but my code runs as far as starting up the app and showing the four buttons, but when you click on either button it crashes. I have no other errors.

MainActivity:

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 output:

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

Answer

seems that you're trying to cast Button into an EditText here:

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

which causes

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