单击Android Studio中的按钮时出现问题 这是例外:
09-03 20:40:31.155 30142-30142/techwaresy.math.infinity E/AndroidRuntime: FATAL EXCEPTION: main
Process: techwaresy.math.infinity, PID: 30142
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4646)
at android.view.View$PerformClick.run(View.java:19403)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4646)
at android.view.View$PerformClick.run(View.java:19403)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{4258dc80 VFED..CL ........ 79,132-389,199 #7f0d0052 app:id/A_field}"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:114)
at java.lang.StringToReal.parseDouble(StringToReal.java:263)
at java.lang.Double.parseDouble(Double.java:295)
at techwaresy.math.infinity.Dalta_Equation.calc_dalta_equation(Dalta_Equation.java:29)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4646)
at android.view.View$PerformClick.run(View.java:19403)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
这是我点击按钮时的方法
public void calc (View view) {
double x = 0;
double x1 = 0;
double x2 = 0;
System.out.println("============================================ START START START ===================================");
EditText a_field = (EditText) findViewById(R.id.A_field);
EditText b_field = (EditText) findViewById(R.id.B_field);
EditText c_field = (EditText) findViewById(R.id.C_field);
String a_s = a_field.getText().toString();
String b_s = b_field.getText().toString();
String c_s = c_field.getText().toString();
double a = Double.parseDouble(a_s);
double b = Double.parseDouble(b_s);
double c = Double.parseDouble(c_s);
System.out.println("============================================= GETTED NUM GETTED NUM GETTED NUM ===================");
double dalta = (b * b) - (4 * a * c);
// ---------------------- Intent Here !! ----------------//
Intent solver_call = new Intent (this , dalta_equation_solver.class);
// ########################################## dalta == 0 ##############################################
if (dalta == 0){
x = (-b) / (2*a);
String msg = "إن للمعادلة حل واحد";
String t1_intent = "= x";
String s1_intent = "" + x;
// ============== For dalta sqrt ==========================
double d_sqrt = Math.sqrt(dalta);
String d_sqrt_txt = " " + d_sqrt ;
// ============== dalta string to pass it to anther activity
String dalta_txt = " " + dalta;
// ---------------------- PutExtra's ============//
solver_call.putExtra(num_of_solution , msg);
solver_call.putExtra(t1 , t1_intent);
solver_call.putExtra(s1 , s1_intent);
solver_call.putExtra(dsqrt , d_sqrt_txt);
solver_call.putExtra(d ,dalta_txt);
solver_call.putExtra(t2 , " ");
solver_call.putExtra(s2 , " ");
startActivity(solver_call);
//######################################dalta > 0######################################
} else if (dalta > 0) {
// ====================== solves ===================
x1 = (-b + Math.sqrt(dalta)) / (2 * a);
x2 = (+b + Math.sqrt(dalta)) / (2 * a);
// ====================== String's =================
String msg = "للمعادلة حلين";
String t1_intent = "= 1X" ;
String t2_intent = "= 2X" ;
String s1_intent = " " + x1;
String s2_intent = " " + x2;
String d_sqrt = " " + (Math.sqrt(dalta));
String d_txt = " " + dalta;
// ====================== PutExtra's ===============
solver_call.putExtra(num_of_solution , msg);
solver_call.putExtra(t1 , t1_intent);
solver_call.putExtra(t2 , t2_intent);
solver_call.putExtra(s1 , s1_intent);
solver_call.putExtra(s2 , s2_intent);
solver_call.putExtra(dsqrt , d_sqrt);
solver_call.putExtra(d , d_txt);
startActivity(solver_call);
} else if (dalta < 0){
// ================= Solve Steps ==================
double b_nigativ = (-b);
double dalta_negativ_sqrt = Math.sqrt(-dalta);
double bottom_part = 2 * a;
x = (b_nigativ / bottom_part);
String x1txt = x + " + i" +dalta_negativ_sqrt;
String x2txt = x + " - i" + dalta_negativ_sqrt;
// ================= Text For Intent ==============
String dalta_sqrt = "i" + dalta_negativ_sqrt;
String msg = "المعادلة مستحيلة الحل في R و لكن لها حلين في C";
String t1_intent = "= 1X";
String t2_intent = "= 2X";
String s1_intent = " " + x1txt;
String s2_intent = " " + x2txt;
String d_txt = " " + dalta;
// ====================== put Extra's =============
solver_call.putExtra(num_of_solution , msg);
solver_call.putExtra(t1 , t1_intent);
solver_call.putExtra(t2 , t2_intent);
solver_call.putExtra(s1 , s1_intent);
solver_call.putExtra(s2 , s2_intent);
solver_call.putExtra(dsqrt , dalta_sqrt);
solver_call.putExtra(d , d_txt);
startActivity(solver_call);
}
}
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="techwaresy.math.infinity.Dalta_Equation">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/enter_all"
android:id="@+id/enter_all_info_dalta"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/a"
android:id="@+id/enter_a_delta_eq"
android:layout_below="@+id/enter_all_info_dalta"
android:layout_toRightOf="@+id/enter_all_info_dalta"
android:layout_toEndOf="@+id/enter_all_info_dalta"
android:layout_marginTop="43dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/A_field"
android:layout_alignBottom="@+id/enter_a_delta_eq"
android:layout_alignLeft="@+id/enter_all_info_dalta"
android:layout_alignStart="@+id/enter_all_info_dalta" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/B_field"
android:layout_marginTop="32dp"
android:layout_below="@+id/A_field"
android:layout_alignLeft="@+id/A_field"
android:layout_alignStart="@+id/A_field" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/C_field"
android:layout_below="@+id/B_field"
android:layout_alignLeft="@+id/B_field"
android:layout_alignStart="@+id/B_field"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/b"
android:id="@+id/enter_b_dalta_equation"
android:layout_alignBottom="@+id/B_field"
android:layout_toRightOf="@+id/enter_all_info_dalta"
android:layout_toEndOf="@+id/enter_all_info_dalta" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/c"
android:id="@+id/enter_c_dalta_equation"
android:layout_alignBottom="@+id/C_field"
android:layout_toRightOf="@+id/enter_all_info_dalta"
android:layout_toEndOf="@+id/enter_all_info_dalta" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calc"
android:id="@+id/calc_equation"
android:onClick="calc"
android:layout_below="@+id/C_field"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
</RelativeLayout>
抱歉,但代码中有一些阿拉伯语字符串但不要错过:)
答案 0 :(得分:0)
由于你没有显示你的xml代码,我无法给出更好的答案,但试试这个
Button button= (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//hendl action here eg
public void calc()
{
}
}
});
答案 1 :(得分:0)
String a_s = a_field.getText().toString();
String b_s = b_field.getText().toString();
String c_s = c_field.getText().toString();
double a = Double.parseDouble(a_s);
double b = Double.parseDouble(b_s);
double c = Double.parseDouble(c_s);
在这一行 我认为您解析具有空格“”或字符的字符串,您必须在调用之前验证字符串格式是否为double:Double.parseDouble(...)。 也许你试试这个在Double.parseDouble之前删除字符串中的空格:
a_s = a_s.replaceAll("\\s","");
b_s = b_s.replaceAll("\\s","");
c_s = c_s.replaceAll("\\s","");
答案 2 :(得分:0)
此问题是由于类型转换或数据转换错误所致,请谨慎转换