2012-02-22 40 views
0

我终于在这么长的时间之后编写了我的Android计算器。但计算器甚至不会启动。我会发布logcat,但我不知道如何去做。这里是我的代码:Android计算器将无法运行

///////////////////// //////////////////////

package rechee.cool; 

import android.app.Activity; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 




public class HelloAndroidActivity extends Activity { 


    /** Called when the activity is first created. */ 
    public EditText display; 

    double total1=0.0; 
    double total2=0.0; 
    char theOperator; 
    public String buttonText; 
    public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide, ButtonMinus; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     display= (EditText) findViewById(R.id.editText1); 
     } 

     String display1= display.getText().toString(); 
     double displayValue= Double.parseDouble(display1); 

     public void getOperator(String btnText){ 
      theOperator = btnText.charAt(0); 



      total1+=displayValue; 
      display.setText(""); 
     } 


      public void onClick(View v) { 
       switch(v.getId()){ 
        case R.id.bOne: 

         display.append("1"); 
         break; 
        case R.id.bTwo: 

         display.append("2"); 
         break; 
        case R.id.bThree: 
         display.append("3"); 
         break; 
        case R.id.bFour: 
         display.append("4"); 
         break; 
        case R.id.bFive: 
         display.append("5"); 
         break; 
        case R.id.bSix: 
         display.append("6"); 
         break; 

        case R.id.bSeven: 
         display.append("7"); 
         break; 
        case R.id.bEight: 
         display.append("8"); 
         break; 
        case R.id.bNine: 
         display.append("9"); 
         break; 
        case R.id.bZero: 
         display.append("0"); 
         break; 
        case R.id.bPoint: 
         display.append("."); 
         break; 
        case R.id.bClear: 
         display.setText(""); 
         break; 
        case R.id.bAdd: 
         buttonText="+"; 
         ButtonAdd= (Button)findViewById(R.id.bAdd); 

         ButtonAdd.setText(buttonText); 


         getOperator(buttonText); 
         break; 
        case R.id.bMinus: 
         buttonText="-"; 
         ButtonMinus= (Button)findViewById(R.id.bMinus); 
         ButtonMinus.setText(buttonText); 
         getOperator(buttonText); 
         break; 
        case R.id.bMultiply: 
         buttonText="*"; 
         ButtonMultiply= (Button)findViewById(R.id.bMultiply); 
         ButtonMultiply.setText(buttonText); 
         getOperator(buttonText); 
         break; 
        case R.id.bDivide: 
         buttonText="/"; 
         ButtonDivide= (Button)findViewById(R.id.bDivide); 
         ButtonDivide.setText(buttonText); 
         getOperator(buttonText); 
         break; 
        case R.id.bEqual: 

         switch (theOperator){ 
         case '+': 
         total2= total1 + displayValue; 
         break; 
         case '-': 
         total2= total1 - displayValue; 
         break; 
         case '*': 
         total2= total1 * displayValue; 
         break; 
         case '/': 
         total2= total1/displayValue; 

         break; 


         } 
         display.setText(Double.toString(total2)); 
         total1=0; 

         } 

         } 


} 

这里的清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="rechee.cool" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".HelloAndroidActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

而这里的链接的logcat: http://textsnip.com/529359

请帮我一把。

+0

你是如何提取logcat的? – CustomX 2012-02-22 15:16:41

+0

String display1 = display.getText()。toString(); double displayValue = Double.parseDouble(display1); 用xml中的文本初始化显示吗? – jsaye 2012-02-22 15:24:02

+0

下次请限制疯狂的换行数量,这实在太尴尬了...... – Nanne 2012-02-22 15:26:02

回答

0

显示为空,因此....试试这个

String display1; 
double displayValue; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      display= (EditText) findViewById(R.id.editText1); 

     if(display.length()!=0){ 
      display1= display.getText().toString(); 
      displayValue= Double.parseDouble(display1); 
      } 

     } 
+0

工作。非常感谢。但现在我的程序打印总数为0.0。猜猜我会在那里工作一段时间。 – recheej 2012-02-22 15:48:39

+0

你试图更努力,因为它很容易... – 2012-02-22 15:52:23

0

你必须在logcat的一个NullPointerException那里。这个问题可能是在这一行:

String display1= display.getText().toString(); 

,如果下面的代码返回null:

display= (EditText) findViewById(R.id.editText1); 

检查,如果你做了正确的控制的发现,因为你的任务:

String display1= display.getText().toString(); 

不在任何方法(类分配)中,因此LogCat不会显示错误的位置。