java - 为什么 Android 计算器无法使用?

标签 java android

经过这么长时间,我终于完成了我的 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;









                        }

                        }


}

这是 list :

<?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 方法之前 display 为 null。但变量尝试使用它的值。您可能会因此得到 NullPointerException。

编辑2: 如果您在 Eclipse 上开发应用程序,请查看以下内容: http://developer.android.com/guide/developing/debugging/ddms.html

关于java - 为什么 Android 计算器无法使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9389701/

相关文章:

android - Android定位服务(GPS):如果GPS已开启,则无法接收位置更新

android - 同一应用中的 ons-split-view 和 ons-sliding-menu

java - 无法启动 Activity ComponentInfo{com.example.countryselect/com.example.countryselect.OfferSelect}

android - 在 Android 工具栏中创建一个按钮

java - 如何在java中获取目录和子目录中的所有文件的名称

java - 返回行程数组中最常见的指南名称

java - 如何在运行基于 Spring Cloud Function 的 AWS Lambda 时解决空指针异常

java - cordova 构建 android > JDK 1.8 检查失败

java - Bukkit Maven sk89q的命令框架

android - 我卡在我的 Android 程序上了