Android Studio Calculator App - 无法执行android的方法:OnClick

时间:2017-04-09 15:51:37

标签: java android

我是Android开发新手,已经开始构建计算器应用程序。我完成了代码并拥有所有按钮及其功能。代码没有显示任何错误并启动应用程序,但是当我单击一个按钮时,它会给我错误:无法执行android的方法:Onclick。这是我的代码:

public class MainActivity extends AppCompatActivity {


private BigDecimal a,b,calculation;
private TextView answer;
private Double conv1, conv2, sqrt1, sqrtcalc;
private int power;
private String str, result, sign, str2, pow, sqrt;

public void number (View view) {

    Button button = (Button) view;
    str += button.getText().toString();
    answer.setText(str);
    conv1 = Double.parseDouble(str);
    a = BigDecimal.valueOf(conv1);


}

public void operation (View view) {

    Button button = (Button) view;

    sign = button.getText().toString();
    answer.setText(str + ".");
    str = "";

}

public void Calculation (View view) {

    Button button = (Button) view;
    str2 = answer.getText().toString();
    conv2 = Double.parseDouble(str2);
    b = BigDecimal.valueOf(conv2);

    if (sign.contentEquals("+")) {

        calculation = a.add(b);
        result = calculation.toString();

    } else if (sign.contentEquals("-")) {

        calculation = a.subtract(b);
        result = calculation.toString();

    } else if (sign.contentEquals("X")) {

        calculation = a.multiply(b);
        result = calculation.toString();

    } else if (sign.contentEquals("÷")) {

        a.divide(b);
        result = calculation.toString();

    } else if (sign.contentEquals("x^n")){

        pow = b.toString();
        power = Integer.parseInt(pow);
        calculation = a.pow(power);
        result = calculation.toString();

    } else if (sign.contentEquals("√")) {

        sqrt = a.toString();
        sqrt1 = Double.parseDouble(sqrt);
        sqrtcalc = Math.sqrt(sqrt1);
        result = sqrtcalc.toString();

    } else {


    }

    answer.setText(result);

}

public void Clear (View view) {

    Button button = (Button) view;
    answer.setText("");
    str = "";
    str2 = "";
    result = "";
    pow = "";
    sign = "";
    sqrt = "";

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    answer = (TextView) findViewById(R.id.textView2);

}
}

这是我的所有代码,我不会插入XML布局:

<?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:id="@+id/activity_main"
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="com.tejasmehta.tejascalc.MainActivity">

<Button
    android:text="3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button5"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_toRightOf="@+id/button2"
    android:layout_toEndOf="@+id/button2"
    android:onClick="number" />

<Button
    android:text="2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_toRightOf="@+id/button1"
    android:layout_toEndOf="@+id/button1"
    android:onClick="number" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button3"
    android:text="+"
    android:layout_alignBaseline="@+id/button5"
    android:layout_alignBottom="@+id/button5"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="operation" />

<Button
    android:text="5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button7"
    android:layout_alignBaseline="@+id/button4"
    android:layout_alignBottom="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:layout_toEndOf="@+id/button4"
    android:onClick="number" />

<Button
    android:text="6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button8"
    android:layout_alignBaseline="@+id/button7"
    android:layout_alignBottom="@+id/button7"
    android:layout_toRightOf="@+id/button7"
    android:layout_toEndOf="@+id/button7"
    android:onClick="number" />

<Button
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button9"
    android:layout_alignBaseline="@+id/button8"
    android:layout_alignBottom="@+id/button8"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="operation" />

<Button
    android:text="8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button6"
    android:layout_alignBaseline="@+id/button14"
    android:layout_alignBottom="@+id/button14"
    android:layout_toRightOf="@+id/button14"
    android:layout_toEndOf="@+id/button14"
    android:onClick="number" />

<Button
    android:text="7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button14"
    android:layout_alignBaseline="@+id/button11"
    android:layout_alignBottom="@+id/button11"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="number" />

<Button
    android:text="x "
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button12"
    android:layout_below="@+id/button8"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="17dp"
    android:onClick="operation" />

<Button
    android:text="9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button11"
    android:layout_alignBaseline="@+id/button12"
    android:layout_alignBottom="@+id/button12"
    android:layout_toRightOf="@+id/button6"
    android:layout_toEndOf="@+id/button6"
    android:onClick="number" />

<Button
    android:text="4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button4"
    android:layout_marginTop="18dp"
    android:layout_below="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="number" />

<Button
    android:text="0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button10"
    android:layout_marginTop="21dp"
    android:layout_below="@+id/button6"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/button11"
    android:layout_toStartOf="@+id/button11"
    android:onClick="number" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button21"
    android:layout_alignBaseline="@+id/button10"
    android:layout_alignBottom="@+id/button10"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="÷"
    android:onClick="operation" />

<Button
    android:text="."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button20"
    android:layout_alignBaseline="@+id/button10"
    android:layout_alignBottom="@+id/button10"
    android:layout_toRightOf="@+id/button10"
    android:layout_toEndOf="@+id/button10"
    android:onClick="number" />

<Button
    android:text="√"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button10"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="24dp"
    android:id="@+id/button22"
    android:onClick="operation" />

<Button
    android:text="x^n"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button22"
    android:layout_toRightOf="@+id/button22"
    android:layout_toEndOf="@+id/button22"
    android:id="@+id/button23"
    android:onClick="operation" />

<Button
    android:text="C"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button24"
    android:layout_alignBaseline="@+id/button23"
    android:layout_alignBottom="@+id/button23"
    android:layout_toRightOf="@+id/button23"
    android:layout_toEndOf="@+id/button23"
    android:onClick="Clear" />

<Button
    android:text="="
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button24"
    android:layout_toRightOf="@+id/button24"
    android:layout_toEndOf="@+id/button24"
    android:id="@+id/button25"
    android:onClick="Calculation" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:id="@+id/textView2"
    android:textSize="30sp"
    android:layout_marginTop="58dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:text="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:id="@+id/button1"
    android:layout_below="@+id/textView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="number" />

</RelativeLayout>

这是错误消息:

 java.lang.IllegalStateException: Could not execute method for android:onClick
                  at 

 android.support.v7.app.AppCompatViewInflater$ 
DeclaredOnClickListener.onCli
ck(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:5610)
                  at 
android.view.View$PerformClick.run(View.java:22260)
                  at 
android.os.Handler.handleCallback(Handler.java:751)
                  at 
android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at 
android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller. 
run(ZygoteInit.java:865)
                  at 
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at 
 android.support.v7.app.AppCompatViewInflater$ . 
DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5610) 
                  at 
android.view.View$PerformClick.run(View.java:22260) 
                  at 
android.os.Handler.handleCallback(Handler.java:751) 
                  at 
android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at 
android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller. 
run(ZygoteInit.java:865) 
                  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
               Caused by: java.lang.NumberFormatException: For input 
string: "null8"
                  at 
 java.lang.FloatingDecimal.readJavaFormatString . 
 (FloatingDecimal.java:1306)
                  at java.lang.Double.parseDouble(Double.java:547)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5610) 
                  at 
android.view.View$PerformClick.run(View.java:22260) 
                  at 
android.os.Handler.handleCallback(Handler.java:751) 
                  at 
android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at 
android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at 

    com.android.internal.os.ZygoteInit$MethodAndArgsCaller. 
   run(ZygoteInit.java:865) 
                  at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

第二次崩溃:

 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:5610)
                  at 
  android.view.View$PerformClick.run(View.java:22260)
                  at 
  android.os.Handler.handleCallback(Handler.java:751)
                  at 
  android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at 
  android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at 
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller. 
   run(ZygoteInit.java:865)
                  at 
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22260) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
               Caused by: java.lang.NumberFormatException: empty String
                  at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
                  at java.lang.Double.parseDouble(Double.java:547)
                  at com.tejasmehta.tejascalc.MainActivity.Calculation(MainActivity.java:52)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22260) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

很抱歉这是如此广泛,但我已经浏览了其他人,他们的解决方案对我没有帮助。任何帮助将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:0)

在这一行:

 str += button.getText().toString();

您使用Button文本

添加null
  

java.lang.NumberFormatException:对于输入字符串:&#34; null8&#34;

然后会在此行引发异常:conv1 = Double.parseDouble(str);因为null8无法转换为Double

所以在添加到string之前检查:

 public void number(View view) {

    String s = "";
    Button button = (Button) view;
    s = button.getText().toString();
    if (s != null && str!=null) {
        str += s;
        answer.setText(str);
    }
    try {
        conv1 = Double.parseDouble(str);
        a = BigDecimal.valueOf(conv1);
    } catch (Exception e) {

    }
}

答案 1 :(得分:0)

看这里有以下问题

1-当你使用rafsanahmad007的代码时,if条件不符合,这就是为什么当你按下数字按钮时没有发生任何改变....但为什么 好吧,这是因为str是null你没有初始化它所以你必须添加 你的onCreate(Bundle savedInstanceState)方法中的任何地方都有str = ""

当你按下操作时,因为str为空并且你使用了它,所以2-崩溃了...... 所以当你解决1-然后2-自动解决

现在您可以按数字然后操作然后编号全部没有崩溃

希望这有助于xD