簡體   English   中英

為什么Android計算器不起作用?

[英]Why won't Android Calculator Work?

不久之后,我終於完成了對Android計算器的編碼。 但是計算器甚至無法啟動。 我會發布logcat,但我不知道該怎么做。 這是我的代碼:

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

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




        }


        // I think it might have to do something with the following code
        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

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

這段代碼屬於哪里? 您可以考慮將它們移到onCreate()方法中。

編輯:在調用onCreate方法之前, 顯示為null。 但是變量嘗試使用其值。 您可能會為此獲得NullPointerException。

EDIT2:如果要在Eclipse上開發應用程序,請查看以下內容: http : //developer.android.com/guide/developing/debugging/ddms.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM