Java - Android SDK - Unfrotunately <project name> has crashed error

馋奶兔 提交于 2019-12-12 03:29:11

问题


I Have been working on a small app, which can calculate complex numbers for me (division, subtraction, multiplying etc.). I have no apparent compile errors, but when i run the program I get a bunch of run-time errors which I can't understand or know how to fix. Am i missing something obvious? This is my code:

    package complex.OliverV;

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

    public class ComplexNumbersActivity extends Activity {

Button Check;
RadioButton plus, minus, multiply, div;
EditText X1,X2,Y1,Y2;
TextView Ans;
int sign;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Check = (Button) findViewById(R.id.Check);
    plus = (RadioButton) findViewById(R.id.plus);
    minus = (RadioButton) findViewById(R.id.minus);
    multiply = (RadioButton) findViewById(R.id.multiply);
    div = (RadioButton) findViewById(R.id.div);
    Ans = (TextView) findViewById(R.id.Ans);
    plus.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=1;
        }


    });
    minus.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=2;
        }


    });
    multiply.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=3;
        }


    });
    div.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign=4;
        }


    });
    Check.setOnClickListener(new View.OnClickListener(){

        String xs=X1.getText().toString();
        String xss=X2.getText().toString();
        String ys=Y1.getText().toString();
        String yss=Y2.getText().toString();



        double x3,y3;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(xs!="" && xss!="" && ys!="" && yss!="")
            {
            double x1=Double.parseDouble(xs);
            double x2=Double.parseDouble(xss);
            double y1=Double.parseDouble(ys);
            double y2=Double.parseDouble(yss);
            switch(sign)
            {
            case(1):
            {
                x3=x1+x2;
                y3=y1+y2;
            }
            case(2):
            {
                x3=x1-x2;
                y3=y1-y2;
            }
            case(3):
            {
                x3=(x1*x2-y1*y2);
                y3=(x2*y1 + x1*y2);
            }
            case(4):
            {
                if(x2!=0 && y2!=0)
                {
                x3 = (x1 * x2 + y1 * y2) / (x2 * x2 + y2 * y2);
                y3 = (x2 * y1 - x1 * y2) / (x2 * x2 + y2 * y2);
                }
                else
                {
                    Ans.setText("Enter valid numbers!");
                }
            }
        }
            Ans.setText("x="+x3+"y="+y3);
        }
            else
            {
                Ans.setText("Enter valid numbers!");
            }

        }
    });
}

}

I am also adding a screenshot of the errors I get.

Im putting an image, because copying the text looks like a mess. Sorry for the long question and thank you to anyone who can take the time to help me out, I am very grateful for every bit of help. :)

回答1:


initialize X1,X2,Y1,Y2 in your onCreate() as Ted Hopp mentioned in his answer and then

change this:

if(xs!="" && xss!="" && ys!="" && yss!="")

to:

if( (xs != null && !xs.equal("")) && 
    (xss != null && !xss.equal("")) && 
    (ys !== null && !ys.equal("")) && 
    (yss != null &&!yss.equal("")) )



回答2:


The problem appears to be here:

Check.setOnClickListener(new View.OnClickListener(){

    String xs=X1.getText().toString();
    String xss=X2.getText().toString();
    String ys=Y1.getText().toString();
    String yss=Y2.getText().toString();

    . . .
}

When this is executing inside onCreate, the fields X1, etc., have not been initialized. Even if it had, the fields would have only the default text, which is probably not what you want. You should move the assignments into the onClick method of the listener. That way you will be working with the current field contents when the onClick method runs.

In the future, please post the text of the logcat output rather than a screen snapshot. Failing that, at least resize the columns and scroll as necessary so we can see the entire text of the logcat messages. And please identify which line(s) in your code are generating the exception.



来源:https://stackoverflow.com/questions/15098895/java-android-sdk-unfrotunately-project-name-has-crashed-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!