计算器应用程序在运行时崩溃

时间:2016-05-04 07:36:39

标签: java android crash

package com.example.android.kalkulatron;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    TextView nomor1 = (TextView) findViewById(R.id.Nomor1);
    TextView nomor2 = (TextView) findViewById(R.id.Nomor2);
    TextView tanda = (TextView) findViewById(R.id.tanda);
    Double no1 = Double.parseDouble(nomor1.getText().toString());
    Double no2 = Double.parseDouble(nomor2.getText().toString());
    Double sum;
    boolean Ceksum;

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

    private void Fsatu(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "1");
            } else {
                nomor1.setText("1");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "1");
        }


    }

    private void Fdua(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "2");
            } else {
                nomor1.setText("2");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "1");
        }

    }

    private void Ftiga(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "3");
            } else {
                nomor1.setText("2");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "2");
        }

    }



    private void Fempat(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "4");
            } else {
                nomor1.setText("4");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "4");
        }

    }

    private void Flima(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "5");
            } else {
                nomor1.setText("5");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "5");
        }

    }

    private void Fenam(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "6");
            } else {
                nomor1.setText("6");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "6");
        }

    }

    private void Ftujuh(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "7");
            } else {
                nomor1.setText("7");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "7");
        }

    }

    private void Fdelapan(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "8");
            } else {
                nomor1.setText("8");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "8");
        }

    }

    private void Fsembilan(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "9");
            } else {
                nomor1.setText("9");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "9");
        }

    }

    private void Ftitik(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + ".");
            } else {
                nomor1.setText(".");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + ".");
        }

    }

    private void Fnol(View view) {
        if (tanda.getText().length() == 0) {
            if (Ceksum = false){
                nomor1.setText(nomor1 + "0");
            } else {
                nomor1.setText("0");
                Ceksum = false;
            }

        } else {
            nomor2.setText(nomor2 + "0");
        }

    }

    public void Ftambah(View view) {
        tanda.setText("+");
    }

    public void Fkurang(View view) {
        tanda.setText("-");
    }

    public void Fkali(View view) {
        tanda.setText("X");
    }

    public void Fbagi(View view) {
        tanda.setText("÷");
    }

    public void Fclear(View view) {
        tanda.setText("");
        nomor1.setText("");
        nomor2.setText("");
    }

    public void Fequals(View view) {
        DecimalFormat df = new DecimalFormat("#.##");
        if (nomor2.getText().length() != 0) {
            if (tanda.getText() == "+") {
                sum = no1 + no2;
                sum = Double.valueOf(df.format(sum));
                nomor1.setText(""+sum);
            } else if (tanda.getText() == "-") {
                sum = no1 - no2;
                sum = Double.valueOf(df.format(sum));
                nomor1.setText(""+sum);

            } else if (tanda.getText() == "X") {
                sum = no1 * no2;
                sum = Double.valueOf(df.format(sum));
                nomor1.setText(""+sum);
            } else if ((tanda.getText() == "÷") && (nomor2.getText() != "0")) {
                sum = no1 / no2;
                sum = Double.valueOf(df.format(sum));
                nomor1.setText(""+sum);
            }
            tanda.setText("");
            nomor2.setText("");
        } else {
            Toast toast = new Toast(this);
            toast.makeText(getApplicationContext(),
                    "Wrong input, repeat!", Toast.LENGTH_SHORT).show();
            tanda.setText("");
            nomor2.setText("");
            nomor1.setText("");
        }

        Ceksum = true;



    }
}

这是我的Android计算器原型,但是当我在手机上测试它时它会不断崩溃。我这样做只是为了试验和错误,在这里我坚持这个。任何人都能找到我犯的错误吗?非常感谢你提前。 (抱歉英文不好)

05-04 14:24:07.937 5391-5391/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.android.kalkulatron, PID: 5391
                                                 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.kalkulatron/com.example.android.kalkulatron.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2290)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:156)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:211)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5373)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                     at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
                                                     at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145)
                                                     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
                                                     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:42)
                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:186)
                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:168)
                                                     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:508)
                                                     at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:180)
                                                     at com.example.android.kalkulatron.MainActivity.<init>(MainActivity.java:13)
                                                     at java.lang.reflect.Constructor.newInstance(Native Method)
                                                     at java.lang.Class.newInstance(Class.java:1606)
                                                     at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:156) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:211) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5373) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:372) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

1 个答案:

答案 0 :(得分:1)

像这样改变

Double sum;
boolean Ceksum;
TextView nomor1;
TextView nomor2;
TextView tanda;
Double no1 = 0d;
Double no2 = 0d;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nomor1 = (TextView) findViewById(R.id.Nomor1);
    nomor2 = (TextView) findViewById(R.id.Nomor2);
    tanda = (TextView) findViewById(R.id.tanda);
    no1 = Double.parseDouble(nomor1.getText().toString());
    no2 = Double.parseDouble(nomor2.getText().toString());

}