-4

I have A problem when I clicked the button In Android Studio this is the Exception :

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) 

and this is the method that when i click the button it called

    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>

Sorry But the code have some Arabic Strings But Dont Wory :)

3 Answers3

0

Since you did not show your xml code, I'am not able to give a better answer but try this one

    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()
               {
                  }
        }
    });
morten.c
  • 3,414
  • 5
  • 40
  • 45
Hamza
  • 2,017
  • 1
  • 19
  • 40
  • delete this line android:onClick="calc" and add my code in your onCreat – Hamza Sep 03 '16 at 18:15
  • Button button= (Button) findViewById(R.id.calc_equation); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //hendl action here eg public void calc() { } } }); – Hamza Sep 03 '16 at 18:16
  • new problem ?what is? – Hamza Sep 03 '16 at 18:29
  • in Intent Obj Line Method invocation 'btn.setOnClickListener(new View.OnClickListener() { @Override public void on...' may produce 'java.lang.NullPointerException' – Hasan Al-khammash Sep 03 '16 at 18:30
  • I solved this problem ... but after I did this way to make action Off Button it still give me the same Exception :( – Hasan Al-khammash Sep 03 '16 at 18:40
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);

in this lines I think you parse string that have spaces " " or char , you must verify the string format is double before call : Double.parseDouble(...) . maybe you try this to remove spaces in the string before Double.parseDouble :

a_s = a_s.replaceAll("\\s","");
b_s = b_s.replaceAll("\\s","");
c_s = c_s.replaceAll("\\s","");
0

This problem is due to the wrong type casting or conversion of data .So cast carefully

Jeevan Rupacha
  • 3,055
  • 1
  • 15
  • 29