2017-04-09 66 views
0

我是Android開發新手,已開始構建計算器應用程序。我完成了代碼並擁有所有按鈕及其功能。該代碼不顯示任何錯誤,並啓動應用程序罰款,但是當我點擊一個按鈕,它給了我錯誤:無法執行android的方法:Onclick。這裏是我的代碼:Android Studio計算器應用程序 - 無法執行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)  

對不起,我i但是我已經看過了,他們的解決方案並沒有幫助我。任何幫助將不勝感激!謝謝!

回答

0

在這一行:

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

要添加空與按鈕上的文字

java.lang.NumberFormatException: For input string: "null8"

則異常在這一行拋出:conv1 = Double.parseDouble(str);因爲null8不能轉換到雙

所以添加到字符串前檢查:

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) { 

    } 
} 
+0

這工作,但是當我輸入計算器中的數字,它顯示爲空(編號),然後當我手術和第二個號碼,它崩潰。爲什麼? –

+0

我編輯了條件...'if(s!= null && str!= null)'...併發布第二次崩潰的日誌.. – rafsanahmad007

+0

第二次崩潰 –

0

看看這裏有你有

1-當u使用rafsanahmad007的代碼,如果條件不滿足,這就是爲什麼沒有變化發生時ü上按下數字鍵下面的問題....但爲什麼 好,這是因爲str爲nullü沒有初始化,所以你必須在你的onCreate(捆綁savedInstanceState)方法添加 這str = ""任何地方

2 - 崩潰當U按下操作好,因爲str爲null和u使用它。 .. 所以當你解決1--然後2-自動解決

現在你可以按號碼,然後操作則數都沒有崩潰

希望這可以幫助的xD

相關問題