In Eclipse AVD I get a “Unfortunately, Calculator

2019-08-24 22:14发布

问题:

My test application is called Calculator, but I get an "Unfortunately, Calculator has stopped." error in AVD. I'm running my Android Virtual Device with API Level 16. This is my code : (No syntax errors as far as Eclipse says)

MainActivity.java :

    package com.papps.calculator;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    import android.widget.Button;

    public class MainActivity extends Activity {
    int num1 = 90;
    int num2 = 90;
    int op = 0;
    int ans = 0;

    TextView answer = (TextView) findViewById(R.id.answer);

    Button ButtonEquals = (Button) findViewById(R.id.ButtonEquals);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    //ONE
    public void none() {
        if(num1 == 90) {
            num1 = 1;
        }
        else if(num2 == 90) {
            num2 = 1;
         }
    }
    //TWO
    public void ntwo() {
        if(num1 == 90) {
            num1 = 2;
        }
        else if(num2 == 90) {
            num2 = 2;
         }
    }
    //THREE
    public void nthree() {
        if(num1 == 90) {
            num1 = 3;
        }
        else if(num2 == 90) {
            num2 = 3;
         }
    }
    //FOUR
    public void nfour() {
        if(num1 == 90) {
            num1 = 4;
        }
        else if(num2 == 90) {
            num2 = 4;
         }
    }
    //FIVE
    public void nfive() {
        if(num1 == 90) {
            num1 = 5;
        }
        else if(num2 == 90) {
            num2 = 5;
         }
    }
    //SIX
    public void nsix() {
        if(num1 == 90) {
            num1 = 6;
        }
        else if(num2 == 90) {
            num2 = 6;
         }
    }
    //SEVEN
    public void nseven() {
        if(num1 == 90) {
            num1 = 7;
        }
        else if(num2 == 90) {
            num2 = 7;
         }
    }
    //EIGHT
    public void neight() {
        if(num1 == 90) {
            num1 = 8;
        }
        else if(num2 == 90) {
            num2 = 8;
         }
    }
    //NINE
    public void nnine() {
        if(num1 == 90) {
            num1 = 9;
        }
        else if(num2 == 90) {
            num2 = 9;
         }
    }
    //ZERO
    public void nzero() {
        if(num1 == 90) {
            num1 = 0;
        }
        else if(num2 == 90) {
            num2 = 0;
         }
    }
    //operators

    //MULTIPLY
    public void multiply() {
        op = 1;
    }
    //Divide
    public void divide() {
        op = 2;
    }
    //Add
    public void add() {
        op = 3;
    }
    //Subtract
    public void subtract() {
        op = 4;
    }

    //EQUALS
    //TextView answer = (TextView) findViewById(R.id.answer);
    //Button ButtonEquals = (Button) findViewById(R.id.ButtonEquals);
    public void equals() {
        if(op == 1){
            answer.setText(num1 * num2);
        }
        else if(op == 2) {
            answer.setText(num1 / num2);
        }
        else if(op == 3) {
            answer.setText(num1 + num2);
        }
        else if(op == 4) {
            answer.setText(num1 - num2);
        }
    }
    }

activity_main.xml :


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="97dp"
        android:layout_marginTop="16dp"
        android:text="@string/app_name"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="15dp"
        android:text="1"
        android:onClick = "none" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginLeft="15dp"
        android:text="2"
        android:onClick = "ntwo" />

    <Button
        android:id="@+id/Button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button2"
        android:layout_alignBottom="@+id/Button2"
        android:layout_marginLeft="34dp"
        android:layout_toRightOf="@+id/Button2"
        android:text="3"
        android:onClick = "nthree" />

    <Button
        android:id="@+id/Button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="36dp"
        android:text="4"
        android:onClick = "nfour" />

    <Button
        android:id="@+id/Button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button4"
        android:layout_alignBottom="@+id/Button4"
        android:layout_alignLeft="@+id/Button2"
        android:text="5"
        android:onClick = "nfive" />

    <Button
        android:id="@+id/Button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button5"
        android:layout_alignBottom="@+id/Button5"
        android:layout_alignLeft="@+id/Button3"
        android:text="6" 
        android:onClick = "nsix" />

    <Button
        android:id="@+id/Button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button4"
        android:layout_centerVertical="true"
        android:text="7" 
        android:onClick = "nseven" />

    <Button
        android:id="@+id/Button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/Button3"
        android:text="8" 
        android:onClick = "neight" />

    <Button
        android:id="@+id/Button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button6"
        android:layout_centerVertical="true"
        android:text="9"
        android:onClick = "nnine" />

    <Button
        android:id="@+id/Button0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button8"
        android:layout_below="@+id/Button8"
        android:layout_marginTop="29dp"
        android:text="0"
        android:onClick = "nzero" />

    <Button
        android:id="@+id/ButtonM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="X"
        android:onClick = "multiply" />

    <Button
        android:id="@+id/ButtonD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/Button7"
        android:text="/"
        android:onClick = "divide" />

    <Button
        android:id="@+id/ButtonS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/textView1"
        android:text="-"
        android:onClick = "subtract" />

    <Button
        android:id="@+id/ButtonA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/ButtonS"
        android:text="+"
        android:onClick = "add" />

    <TextView
        android:id="@+id/answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ButtonD"
        android:layout_marginBottom="24dp"
        android:layout_toRightOf="@+id/ButtonD"
        android:text="Answer"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/ButtonEquals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/answer"
        android:layout_alignBottom="@+id/answer"
        android:layout_toLeftOf="@+id/Button0"
        android:text="="
        android:onClick = "equals" />

</RelativeLayout>

Any help is appreciated! I can't figure out what's wrong.

回答1:

Your problem most likely lies with the global View variables. Modify

TextView answer = (TextView) findViewById(R.id.answer);

    Button ButtonEquals = (Button) findViewById(R.id.ButtonEquals);

so it is just

TextView answer;
Button ButtonEquals;

Then in onCreate(), do this:

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

    answer = (TextView) findViewById(R.id.answer);
    ButtonEquals = (Button) findViewById(R.id.ButtonEquals);
    }

Your app is probably crashing because of the fact that you are trying to access the Views before the Activity has anything to show. Putting your code after setContentView allows the Views to be found and you avoid NullPointerExceptions

Also:

Your methods need to be methodName (View view) if they are referenced from the onClick: attribute

Most your methods need to be changed, but I'll take the ButtonEquals as an example. Your xml says

android:onClick = "equals"

So the method in your code needs to be

public void equals (View v)

Adding the View argument needs to be done for every method that the onClick: attribute uses.



回答2:

1/ You can see exactly what's wrong by reading the logcat. It can be seen from Eclipse from the Java perspective or by using the command line:

adb logcat

2/ Move the instantiation of the *TextView*s to the onCreate() method after the setContentView() call.