2014-02-11 54 views
-1

我已經做其中CheckBox檢查不同的動作有Button依賴,這裏是代碼:做的東西,如果複選框被選中

public class CostiDiImpianto extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_costi_di_impianto); 
    } 

    public void onClick(View view) { 

     double Onorario1,SpeseD1,CostI1,Iva1,Deb1,Rit1,ccp1,Netto1; 
     // 
     Onorario1 = 0; 
     SpeseD1 = 0; 
     ccp1 = 0; 

     // 
     EditText Onorario = (EditText) findViewById(R.id.Onorario); 
     EditText SpeseD = (EditText) findViewById(R.id.SpeseD); 
     TextView CostI = (TextView) findViewById(R.id.CostI); 
     TextView Iva = (TextView) findViewById(R.id.Iva); 
     TextView Deb = (TextView) findViewById(R.id.Deb); 
     TextView Rit = (TextView) findViewById(R.id.Rit); 
     TextView ccp = (TextView) findViewById(R.id.ccp); 
     TextView Netto = (TextView) findViewById(R.id.Netto); 
     final CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1); 
     final CheckBox checkBox2=(CheckBox)findViewById(R.id.checkBox2); 
     // 
     try{ 
      Onorario1 = Math.round(Double.parseDouble(Onorario.getText().toString())*100.0)/100.0; 
      SpeseD1 = Math.round(Double.parseDouble(SpeseD.getText().toString())*100.0)/100.0; 
     } catch (NumberFormatException e){ 
      ErrorMsg(); 
      Pulisci(view); 
      return; 
     } 

     if(checkBox1.isChecked()) 
        ccp1 = Onorario1 * 2/100; 
        Iva1 = (Onorario1 + ccp1)*22/100; 
        Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
        CostI1 = (Onorario1+ccp1+SpeseD1); 
        Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
        Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
        // 
        ccp.setText(Double.toString(round(ccp1,2))); 
        Iva.setText(Double.toString(round(Iva1,2))); 
        Rit.setText(Double.toString(round(Rit1,2))); 
        CostI.setText(Double.toString(round(CostI1,2))); 
        Deb.setText(Double.toString(round(Deb1,2))); 
        Netto.setText(Double.toString(round(Netto1,2))); 
     if(checkBox2.isChecked()) 
        ccp1 = Onorario1 * 4/100; 
        Iva1 = (Onorario1 + ccp1)*22/100; 
        Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
        CostI1 = (Onorario1+ccp1+SpeseD1); 
        Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
        Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
        // 
        ccp.setText(Double.toString(round(ccp1,2))); 
        Iva.setText(Double.toString(round(Iva1,2))); 
        Rit.setText(Double.toString(round(Rit1,2))); 
        CostI.setText(Double.toString(round(CostI1,2))); 
        Deb.setText(Double.toString(round(Deb1,2))); 
        Netto.setText(Double.toString(round(Netto1,2))); 



     } 




    private void Pulisci(View view) { 
     EditText Onorario = (EditText) findViewById(R.id.Onorario); 
     EditText SpeseD = (EditText) findViewById(R.id.SpeseD); 
     TextView CostI = (TextView) findViewById(R.id.CostI); 
     TextView Iva = (TextView) findViewById(R.id.Iva); 
     TextView Deb = (TextView) findViewById(R.id.Deb); 
     TextView Rit = (TextView) findViewById(R.id.Rit); 
     TextView ccp = (TextView) findViewById(R.id.ccp); 
     TextView Netto = (TextView) findViewById(R.id.Netto); 
     Onorario.setText(""); 
     SpeseD.setText(""); 
     CostI.setText(""); 
     Iva.setText(""); 
     Deb.setText(""); 
     Rit.setText(""); 
     ccp.setText(""); 
     Netto.setText(""); 

    } 

    private void ErrorMsg() { 
     AlertDialog Msg = new AlertDialog.Builder(this).create(); 
     Msg.setTitle("Errore"); 
     Msg.setMessage("Hai inserito dei dati non validi!"); 
     Msg.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      }}); 
     Msg.show(); 
    } 
    public static double round(double value, int places) { 
     if (places < 0) throw new IllegalArgumentException(); 

     long factor = (long) Math.pow(10, places); 
     value = value * factor; 
     long tmp = Math.round(value); 
     return (double) tmp/factor; 
    } 
    } 

但是,當我點擊clickbox程序中止。
我在做什麼錯?

02-11 13:20:08.571: D/gralloc_goldfish(549): Emulator without GPU emulation detected. 
02-11 13:20:10.391: I/dalvikvm(549): threadid=3: reacting to signal 3 
02-11 13:20:10.450: I/dalvikvm(549): Wrote stack traces to '/data/anr/traces.txt' 
02-11 13:20:11.840: D/AndroidRuntime(549): Shutting down VM 
02-11 13:20:11.840: W/dalvikvm(549): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
02-11 13:20:11.880: E/AndroidRuntime(549): FATAL EXCEPTION: main 
02-11 13:20:11.880: E/AndroidRuntime(549): java.lang.IllegalStateException: Could not find a method onCheckboxClicked(View) in the activity class com.ITE.economiaaziendale.CostiDiImpianto for onClick handler on view class android.widget.CheckBox with id 'checkBox2' 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.view.View$1.onClick(View.java:3031) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.view.View.performClick(View.java:3511) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.widget.CompoundButton.performClick(CompoundButton.java:100) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.view.View$PerformClick.run(View.java:14105) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.os.Handler.handleCallback(Handler.java:605) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.os.Looper.loop(Looper.java:137) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.app.ActivityThread.main(ActivityThread.java:4424) 
02-11 13:20:11.880: E/AndroidRuntime(549): at java.lang.reflect.Method.invokeNative(Native Method) 
02-11 13:20:11.880: E/AndroidRuntime(549): at java.lang.reflect.Method.invoke(Method.java:511) 
02-11 13:20:11.880: E/AndroidRuntime(549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-11 13:20:11.880: E/AndroidRuntime(549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-11 13:20:11.880: E/AndroidRuntime(549): at dalvik.system.NativeStart.main(Native Method) 
02-11 13:20:11.880: E/AndroidRuntime(549): Caused by: java.lang.NoSuchMethodException: onCheckboxClicked [class android.view.View] 
02-11 13:20:11.880: E/AndroidRuntime(549): at java.lang.Class.getConstructorOrMethod(Class.java:460) 
02-11 13:20:11.880: E/AndroidRuntime(549): at java.lang.Class.getMethod(Class.java:915) 
02-11 13:20:11.880: E/AndroidRuntime(549): at android.view.View$1.onClick(View.java:3024) 
02-11 13:20:11.880: E/AndroidRuntime(549): ... 12 more 
+2

請提供一個堆棧跟蹤。順便說一句,你應該添加一個偵聽器到你的複選框,如果你想根據事件做出動作 – nikis

+0

你也可以使用radiogroup .. @ user3293120 – PSK

+0

請分享完整的堆棧跟蹤 –

回答

2

你應該試試這個:

satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 

    } 
} 
0

onClick()函數被調用onCheckboxClicked(View)。但是,您正在public void Calcola(View view)之內實現其功能,所以基本上它們不匹配,Android會確定onClick()事件函數不存在,所以這就是爲什麼你會得到該異常。

只需重命名其中的一個(佈局中的onClick()函數名稱或函數名稱),它就可以工作。

相關問題