2013-07-24 120 views
-1

我已經在android中創建了一個應用程序,它有兩個編輯文本,用於爲運算符(如減法,加法,乘法和除法)計算數字和幾個單選按鈕以計算在文本視圖中的結果,我的問題是當我只在編輯文本中輸入一個數字,然後單擊計算按鈕我的模擬器關閉,該怎麼辦?顯示「不幸您的應用程序已停止」消息

07-24 19:04:40.250: I/Choreographer(1672): Skipped 44 frames! The application may be doing too much work on its main thread. 
07-24 19:04:40.802: I/Choreographer(1672): Skipped 44 frames! The application may be doing too much work on its main thread. 
07-24 19:04:42.522: I/Choreographer(1672): Skipped 45 frames! The application may be doing too much work on its main thread. 
07-24 19:04:43.070: I/Choreographer(1672): Skipped 57 frames! The application may be doing too much work on its main thread. 
07-24 19:04:43.320: D/AndroidRuntime(1672): Shutting down VM 
07-24 19:04:43.320: W/dalvikvm(1672): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  FATAL EXCEPTION: main 
07-24 19:04:43.439: E/AndroidRuntime(1672):  java.lang.NumberFormatException: Invalid int: "" 
07-24 19:04:43.439: E/AndroidRuntime(1672):  at java.lang.Integer.invalidInt(Integer.java:138) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  at java.lang.Integer.parseInt(Integer.java:359) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  at java.lang.Integer.parseInt(Integer.java:332) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  at com.appwacky.calcumachine.MainActivity$1.onClick(MainActivity.java:61) 

07-24 19:04:43.439: E/AndroidRuntime(1672):  atAndroid.view.View.performClick(View.java:4204) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  atAndroid.view.View$PerformClick.run(View.java:17355) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  atAndroid.os.Handler.handleCallback(Handler.java:725) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  atAndroid.os.Handler.dispatchMessage(Handler.java:92) 
07-24 19:04:43.439: E/AndroidRuntime(1672):  atAndroid.os.Looper.loop(Looper.java:137) 
07-24 19:04:43.439: E/AndroidRuntime(1672): Android.app.ActivityThread.main(ActivityThread.java:5041) 

07-24 19:04:43.439: E/AndroidRuntime(1672):  at java.lang.reflect.Method.invokeNative(Native Method) 

07-24 19:04:43.439: E/AndroidRuntime(1672):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-24 19:04:43.439: E/AndroidRuntime(1672):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 

07-24 19:04:43.439: E/AndroidRuntime(1672): at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 

07-24 19:04:43.439: E/AndroidRuntime(1672):  at dalvik.system.NativeStart.main(Native Method) 

_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

package com.appwacky.calcumachine; 

import org.w3c.dom.Text; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity 
{ 

    int i,j,res; 

    EditText edt1,edt2; 
    TextView tv1; 
    RadioButton add,sub,mul,div; 
    Button b1; 



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

     edt1=(EditText)findViewById(R.id.insert1); 
     edt2=(EditText)findViewById(R.id.insert2); 

     tv1=(TextView)findViewById(R.id.textView1); 

     add=(RadioButton)findViewById(R.id.radio0); 
     sub=(RadioButton)findViewById(R.id.radio1); 
     mul=(RadioButton)findViewById(R.id.radio2); 
     div=(RadioButton)findViewById(R.id.radio3); 

     b1=(Button)findViewById(R.id.button1); 

     edt1.getText().toString(); 

     edt1.requestFocus();    
     edt2.getText().toString(); 



     b1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) 

      { 

       if(edt1.length()==0 && edt2.length()==0) 
       { 
        Toast.makeText(getApplicationContext(), "Please enter some number", Toast.LENGTH_LONG).show(); 
       } 


       i=Integer.parseInt(edt1.getText().toString()); 
        j=Integer.parseInt(edt2.getText().toString()); 

       if (add.isChecked()) 
       { 

        res=i+j; 

        //Integer.toString(res); 

        tv1.setText(Integer.toString(res)); 

        System.out.println(tv1); 


       } 

       if (sub.isChecked()) 
       { 
        res=i-j; 

        tv1.setText(Integer.toString(res)); 
        System.out.println(tv1); 

       } 




       if(mul.isChecked()) 
       { 

        res=i*j; 

        tv1.setText(Integer.toString(res)); 
        System.out.println(tv1); 

       } 

      try{ 

       if(div.isChecked()) 
       { 
        tv1.setText(""); 
        res=i/j; 

        tv1.setText(Integer.toString(res)); 
        System.out.println(tv1); 
       } 

      } 
      catch(ArithmeticException ae) 
      { 
       System.out.println("Its an arithmatic error"); 
       Toast.makeText(getApplicationContext(), "This is against the arithmatic law", Toast.LENGTH_LONG).show(); 
      } 


      } 
     }); 






    } 


} 
+3

最有可能的解析錯誤,因爲其他'EditText'不包含值,但不可能說沒有logcat和相關的代碼 – codeMagic

+0

Logcat和代碼所需的堆棧跟蹤引用。 –

+0

請發佈代碼片段和堆棧跟蹤讓我們看看。這就像告訴你的機械師你的車有什麼問題,但沒有帶來汽車。 :) –

回答

-2

Unfortunately your application has stopped - 消息就像第一次哭泣的孩子,當它來到世界上。你的應用程序哭了,想看看你的。 :)

Jocking

請採取使用EditText看看吧。

第1步:創建您的實例像Edittext ed=(Edittext)findViewById(R.id.editetext1) 第2步:然後你就可以使用ed.getText();

請注意,從onClick();方法使用它,如果你只創建EditText參考,但沒有對象,那麼你將得到null pointer exception

+0

,你的意思是'參考'實例實際上是一個對象。 – njzk2

+0

是的你是對的。我剛剛更新了我的答案。 – Mahmudul

+0

實際上,當我在一個編輯文本中輸入一個數字,而不是輸入其他編輯文本....然後點擊按鈕來計算,整個應用程序就會停止 – rishiPlus

相關問題