gpt4 book ai didi

Java - Android SDK - 不幸的是<项目名称>发生了崩溃错误

转载 作者:行者123 更新时间:2023-12-01 23:59:29 24 4
gpt4 key购买 nike

我一直在开发一个小应用程序,它可以为我计算复数(除法、减法、乘法等)。我没有明显的编译错误,但是当我运行程序时,我收到一堆运行时错误,我无法理解或不知道如何修复。我错过了一些明显的东西吗?这是我的代码:

    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!");
}

}
});
}

}

我还添加了我收到的错误的屏幕截图。 Errors我放了一张图片,因为复制文本看起来很乱。很抱歉问了这么长的问题,并感谢任何能花时间帮助我的人,我非常感谢每一个帮助。 :)

最佳答案

按照 Ted Hopp 在他的回答中提到的那样,在 onCreate() 中初始化 X1,X2,Y1,Y2 ,然后

更改此:

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

至:

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

关于Java - Android SDK - 不幸的是<项目名称>发生了崩溃错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com