gpt4 book ai didi

java - 致命异常主要

转载 作者:行者123 更新时间:2023-12-01 13:38:42 25 4
gpt4 key购买 nike

我正在构建一个转换不同单位的应用程序,并且收到“致命异常主”消息,但我的代码运行至启动应用程序并显示四个按钮,但是当您单击任一按钮时崩溃。我没有其他错误。

主要 Activity :

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

计算长度:

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

关于java - 致命异常主要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com