更新*我删除了女性,男性按钮,并按照建议添加了一个复选框,但现在再次崩溃*。没有语法错误。为什么要崩溃???请帮忙。
这是代码: 我的代码有几个问题,如果你能帮助我,我真的很感激...
我有一个USMC Initial Strenght测试计算器,它没有显示任何语法错误。问题是在我输入edittext中的所有值之后,当我点击计算它崩溃时。所以我意识到,这与我希望其中一种方法计算得分在textView中显示的事实有关,所以我必须将textView转换为Integer对吗? 而且我还希望根据计算的内容显示在不同的文本视图中显示通过或失败的文本,该方法返回一个字符串。 如何显示返回的String和返回的整数并将其显示在textviews上?
这就是我所拥有的:
package siuc.cs.rosie;
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 USMCISTActivity extends Activity {
/** Called when the activity is first created. */
enum gender{male, female}
private EditText pullUpArm;
private EditText crunches;
private EditText runMins;
private EditText runSecs;
private TextView factor1;
private TextView txtscore;
private TextView result;
Button calculate, male, female, about;
int tempScore;
String tempResult;
maleIst m;
femaleIst f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m = new maleIst();
f = new femaleIst();
pullUpArm=(EditText) findViewById(R.id.editText1);
crunches=(EditText) findViewById(R.id.editText2);
runMins=(EditText) findViewById(R.id.editText4);
runSecs=(EditText) findViewById(R.id.editText3);
factor1=(TextView) findViewById(R.id.textView4);
calculate=(Button) findViewById(R.id.button1);
male=(Button) findViewById(R.id.button2);
female=(Button) findViewById(R.id.button3);
txtscore=(TextView) findViewById(R.id.textView7);
result=(TextView) findViewById(R.id.textView8);
}
public void calculateScore(View v)
{
int armPull = Integer.parseInt(pullUpArm.getText().toString());
int crunchesNum = Integer.parseInt(crunches.getText().toString());
int runmins = Integer.parseInt(runMins.getText().toString());
int runsec = Integer.parseInt(runSecs.getText().toString());
//String resultTxt=result;
String myresult=result.getText().toString();
int score = Integer.parseInt(txtscore.getText().toString());//this is what is making the program crash
if (male.isPressed())
{
m.pullUp = armPull;
m.crunchM = crunchesNum;
m.minutes = runmins;
m.seconds = runsec;
score = m.mIstScore();//Something to do with (score) is what is making it crash
myresult= m.MistState();
}
else if (female.isPressed())
{
f.arm = armPull;
m.crunchM = crunchesNum;
m.minutes = runmins;
m.seconds = runsec;
//score = f.fIstScore();
myresult = f.FistState();
}
result.setText(myresult);
result.setVisibility(1);
//txtscore.setText(Integer.toString(score));
txtscore.setVisibility(1);
//result.setText(myresult);//is this right? I cannot get the result pass or fail to show
}
public void femaleClick(View v)
{
factor1.setText("Arm Hang");
}
public void maleClick(View v)
{
factor1.setText("Pull-Ups");
}
}
这是LOGCAT:
07-30 22:00:16.826:W / KeyCharacterMap(266):没有用于id 0的键盘
07-30 22:00:16.826:W / KeyCharacterMap(266):使用默认键映射: /system/usr/keychars/qwerty.kcm.bin
07-30 22:00:22.006:D / AndroidRuntime(266):关闭虚拟机
07-30 22:00:22.006:W / dalvikvm(266):threadid = 1:线程退出 未捕获的异常(组= 0x4001d800)
07-30 22:00:22.037:E / AndroidRuntime(266):致命异常:主
07-30 22:00:22.037:E / AndroidRuntime(266): java.lang.IllegalStateException:无法执行的方法 活性
07-30 22:00:22.037:E / AndroidRuntime(266):at android.view.View $ 1.onClick(View.java:2072)
07-30 22:00:22.037:E / AndroidRuntime(266):at android.view.View.performClick(View.java:2408)
07-30 22:00:22.037:E / AndroidRuntime(266):at android.view.View $ PerformClick.run(View.java:8816)
07-30 22:00:22.037:E / AndroidRuntime(266):at android.os.Handler.handleCallback(Handler.java:587)
07-30 22:00:22.037:E / AndroidRuntime(266):at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 22:00:22.037:E / AndroidRuntime(266):at android.os.Looper.loop(Looper.java:123)
07-30 22:00:22.037:E / AndroidRuntime(266):at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 22:00:22.037:E / AndroidRuntime(266):at java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:00:22.037:E / AndroidRuntime(266):at java.lang.reflect.Method.invoke(Method.java:521)
07-30 22:00:22.037:E / AndroidRuntime(266):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 22:00:22.037:E / AndroidRuntime(266):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 22:00:22.037:E / AndroidRuntime(266):at dalvik.system.NativeStart.main(原生方法)
07-30 22:00:22.037:E / AndroidRuntime(266):引起: java.lang.reflect.InvocationTargetException
07-30 22:00:22.037:E / AndroidRuntime(266):at siuc.cs.rosie.USMCISTActivity.calculateScore(USMCISTActivity.java:61)
07-30 22:00:22.037:E / AndroidRuntime(266):at java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:00:22.037:E / AndroidRuntime(266):at java.lang.reflect.Method.invoke(Method.java:521)
07-30 22:00:22.037:E / AndroidRuntime(266):at android.view.View $ 1.onClick(View.java:2067)
07-30 22:00:22.037:E / AndroidRuntime(266):... 11更多
07-30 22:00:22.037:E / AndroidRuntime(266):引起: java.lang.NumberFormatException:无法解析''为整数
07-30 22:00:22.037:E / AndroidRuntime(266):at java.lang.Integer.parseInt(Integer.java:412)
07-30 22:00:22.037:E / AndroidRuntime(266):at java.lang.Integer.parseInt(Integer.java:382)
07-30 22:00:22.037:E / AndroidRuntime(266):... 15更多
以下是其他课程:
MALE CLASS:
package siuc.cs.rosie;
public class maleIst{
int minutes;
int seconds;
int pullUp;
int pullUpScore;
boolean pullUpState;
int crunchM;
int crunchScore;
boolean crunchState;
int s=73;
int high=809;
int low=800;
int run;
int mrunScore;
boolean done=false;
boolean mrunState;
String str=new String ();
public int getPullUp()
{
return pullUp;
}
public void setPullUp(int pullUp)
{
this.pullUp = pullUp;
}
public int getcrunchM()
{
return crunchM;
}
public void setcrunchM(int crunchM)
{
this.crunchM = crunchM;
}
public int calculatePullUp()
{
if (pullUp<3)
{
pullUpScore=0;
pullUpState=false;
}
else if (pullUp>20)
{
pullUpScore=100;
pullUpState=true;
}
else if (pullUp>=3 && pullUp<=19)
{
pullUpScore=pullUp*5;
pullUpState=true;
}
return pullUpScore;
}
public boolean PullUpState()
{
return pullUpState;
}
public int calculateCrunch()
{
if (crunchM<44)
{
crunchScore=0;
crunchState=false;
}
else if (crunchM>=44 && crunchM<=100)
{
crunchScore=crunchM;
crunchState=true;
}
else if (crunchM>100)
{
crunchScore=100;
crunchState=true;
}
return crunchScore;
}
public boolean CrunchState()
{
return crunchState;
}
public int calculateMRun()
{
int run=seconds+(minutes*60);
while (!done)
{
if (run<=540)
{
mrunScore=100;
done=true;
mrunState=true;
}
else if (run>810)
{
mrunScore=0;
done=true;
mrunState=false;
}
else if (run>540 && run<550)
{
mrunScore=99;
done=true;
mrunState=true;
}
else if (run>=801 && run<=810)
{
mrunScore=73;
done=true;
mrunState=true;
}
else if (low<=run && run<=high)
{
mrunScore=s;
done=true;
mrunState=true;
}
else
{
s++;
high-=10;
low-=10;
mrunState=true;
}
}
return mrunScore;
}
public boolean MRunState()
{
return mrunState;
}
public String MistState()
{
if (PullUpState()==true && CrunchState()==true && MRunState()==true)
str="Pass";
else
str="Fail";
return str;
}
public int mIstScore()
{
int finalScore=pullUpScore+crunchScore+mrunScore;
return finalScore;
}}
女性课程:
package siuc.cs.rosie;
public class femaleIst {
int run;
int armScore;
boolean armState;
int arm;
int crunchF;
int crunchScore;
boolean crunchState;
boolean frunState;
int s=72;
int high=889;
int low=880;
int frunScore;
boolean done=false;
String str=new String ();
int minutes;
int seconds;
public int getArmHang()
{
return arm;
}
public void setarmHang(int arm)
{
this.arm = arm;
}
public int getcrunchF()
{
return crunchF;
}
public void setcrunchF(int crunchF)
{
this.crunchF = crunchF;
}
public int calculateHang()
{
if(arm<12)
{
armScore=0;
armState=false;
}
else if (12<=arm && arm<=40)
{
armScore=arm;
armState=true;
}
else if (40<arm && arm<=69)
{
int temp=arm-40;
armScore=40+(temp*2);
armState=true;
}
else if (arm>=70)
{
armScore=100;
armState=true;
}
return armScore;
}
public boolean hangState()
{
return armState;
}
public int calculateCrunch()
{
if (crunchF<44)
{
crunchScore=0;
crunchState=false;
}
else if (crunchF>=44 && crunchF<=100)
{
crunchScore=crunchF;
crunchState=true;
}
else if (crunchF>100)
{
crunchScore=100;
crunchState=true;
}
return crunchScore;
}
public boolean CrunchState()
{
return crunchState;
}
public int calculatefRun()
{
int run=seconds+(minutes*60);
while (!done)
{
if (run<=630)
{
frunScore=100;
done=true;
frunState=true;
}
else if (run>900)
{
frunScore=0;
done=true;
frunState=false;
}
else if (run>630 && run<640)
{
frunScore=99;
done=true;
frunState=true;
}
else if (run>=890 && run<=900)
{
frunScore=72;
done=true;
frunState=true;
}
else if (low<=run && run<=high)
{
frunScore=s;
done=true;
frunState=true;
}
else
{
s++;
high-=10;
low-=10;
frunState=true;
}
}
return frunScore;
}
public boolean FRunState()
{
return frunState;
}
public String FistState()
{
if (hangState()==true && CrunchState()==true && FRunState()==true)
str="Pass";
else
str="Fail";
return str;
}
public int fIstScore()
{
int finalScore=armScore+crunchScore+frunScore;
return finalScore;
}
}
* 更新代码 ** 主要活动* *
我删除了(从其他“助手”类中扩展了Activity)
public class USMCISTActivity extends Activity {
/** Called when the activity is first created. */
private EditText pullUpArm;
private EditText crunches;
private EditText runMins;
private EditText runSecs;
private TextView factor1;
private TextView txtscore;
private TextView result;
Button calculate, male, female, about;
int tempScore;
String tempResult;
maleIst m;
femaleIst f;
final CheckBox checkBox=(CheckBox) findViewById(R.id.checkBox1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m = new maleIst();
f = new femaleIst();
pullUpArm=(EditText) findViewById(R.id.editText1);
crunches=(EditText) findViewById(R.id.editText2);
runMins=(EditText) findViewById(R.id.editText4);
runSecs=(EditText) findViewById(R.id.editText3);
factor1=(TextView) findViewById(R.id.textView4);
calculate=(Button) findViewById(R.id.button1);
//final CheckBox checkBox=(CheckBox) findViewById(R.id.checkBox1);
txtscore=(TextView) findViewById(R.id.textView7);
result=(TextView) findViewById(R.id.textView8);
}
public void calculateScore(View v)
{
int armPull = Integer.parseInt(pullUpArm.getText().toString());
int crunchesNum = Integer.parseInt(crunches.getText().toString());
int runmins = Integer.parseInt(runMins.getText().toString());
int runsec = Integer.parseInt(runSecs.getText().toString());
String myresult="NA";
int score=0;
//checkBox.setChecked(false);
//String resultTxt=result;
//String myresult=result.getText().toString();
//int score = Integer.parseInt(txtscore.getText().toString());
if (checkBox.isChecked())
{
f.arm = armPull;
m.crunchM = crunchesNum;
m.minutes = runmins;
m.seconds = runsec;
score = f.fIstScore();
myresult = f.FistState();
}
else
{
m.pullUp = armPull;
m.crunchM = crunchesNum;
m.minutes = runmins;
m.seconds = runsec;
score = m.mIstScore();
myresult= m.MistState();
}
result.setText(myresult);
result.setVisibility(1);
txtscore.setText(Integer.toString(score));
txtscore.setVisibility(1);
//result.setText(myresult);//is this right? I cannot get the result pass or fail to show
}
public void femaleClick(View v)
{
factor1.setText("Arm Hang");
}
}
HERE IS MY XML LAYOUT:
<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"
android:background="@color/background" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/enterScores"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/text_color"
android:textSize="25dp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/run1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/text_color" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:text="@string/Crunches"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/text_color" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView5"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:text="@string/pullUps"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/text_color" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView6"
android:layout_alignBottom="@+id/textView6"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="number"
android:text="@string/seconds1" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView6"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/editText2"
android:ems="10"
android:inputType="number"
android:text="@string/minutes1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_marginTop="19dp"
android:onClick="calculateScore"
android:text="@string/calculate" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView3"
android:onClick="femaleClick"
android:text="@string/gender1" />
<ImageView
android:id="@+id/ega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:scaleType="fitCenter"
android:src="@drawable/egablack" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_toLeftOf="@+id/editText3"
android:text="@string/score1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView7"
android:layout_marginTop="23dp"
android:layout_toLeftOf="@+id/editText3"
android:text="@string/testState"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
AND FINALLY AN UPDATED LOGCAT:
07-30 21:35:44.952: D/AndroidRuntime(274): Shutting down VM
07-30 21:35:44.952: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 21:35:45.013: E/AndroidRuntime(274): FATAL EXCEPTION: main
07-30 21:35:45.013: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{siuc.cs.rosie/siuc.cs.rosie.USMCISTActivity}: java.lang.NullPointerException
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 21:35:45.013: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 21:35:45.013: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 21:35:45.013: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 21:35:45.013: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 21:35:45.013: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
07-30 21:35:45.013: E/AndroidRuntime(274): Caused by: java.lang.NullPointerException
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.Activity.findViewById(Activity.java:1637)
07-30 21:35:45.013: E/AndroidRuntime(274): at siuc.cs.rosie.USMCISTActivity.<init>(USMCISTActivity.java:27)
07-30 21:35:45.013: E/AndroidRuntime(274): at java.lang.Class.newInstanceImpl(Native Method)
07-30 21:35:45.013: E/AndroidRuntime(274): at java.lang.Class.newInstance(Class.java:1429)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-30 21:35:45.013: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-30 21:35:45.013: E/AndroidRuntime(274): ... 11 more
07-30 21:35:47.923: I/Process(274): Sending signal. PID: 274 SIG: 9
答案 0 :(得分:1)
m = new maleIst();
f = new femaleIst();
你不能也不能试图这样做。
扩展Activity
的类永远不能使用new
进行实例化。 Activity
是一个特殊案例类,只能由操作系统使用startActivity(...)
或其他startXXX
方法创建。
Activity
永远不应公开任何方法或成员变量以供任何其他应用程序组件使用 - Android根本无法正常工作。
请阅读Application Fundamentals和Activity文档。
编辑:首先,从extends Activity
和maleIst
类定义中删除femaleIst
。它们显然是辅助类,不提供Activity
功能,即它们不用作UI组件。
其次,导致应用程序崩溃的异常是由此行引起的......
int score = Integer.parseInt(txtscore.getText().toString());
...但您永远不会为txtscore
小部件设置任何文字。在我看来,你要么完全删除这一行,要么将其移到你的代码中。
老实说,我真的不确定你的代码是做什么的,但看起来你有一个流程问题,即你做错了。
答案 1 :(得分:0)
07-30 22:00:22.037: E/AndroidRuntime(266): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
错误的上述部分意味着您在代码中的某个位置尝试将空字符串转换为数字。我预计它位于MistState
或FistState
代码中的某个位置。你可以发帖吗?
- 编辑
查看额外的代码,看起来当字段未完成时抛出异常。您正在尝试将空字符串转换为整数。
答案 2 :(得分:0)
从堆栈跟踪看起来,此视图的代码失败
int score = Integer.parseInt(txtscore.getText().toString());
问题可能是你的TextView没有设置任何文本,因此 getText()返回一个空字符串。这将导致整数转换失败,并且数字格式异常无效。
话虽如此,您不需要在该步骤将字符串转换为int。由于您已经可以访问需要设置的TextView,因此您只需要在其上调用 setText(...)方法。
更改这些行
String myresult=result.getText().toString();
int score = Integer.parseInt(txtscore.getText().toString());//this is what is making the program crash
到这个
String myresult = "NA"; // Default text to display
int score = 0; // Default score value
这基本上会让你通过你的程序崩溃。
现在显示没有得分或结果(通过/失败)的原因是因为您尝试使用Button.isPressed()方法来检查它是男性还是女性。
if(male.isPressed()) {
......
} else if(female.isPressed()) {
......
}
因为您点击了“计算”&#39;按钮获得分数,其他任何按钮都不会处于按下状态。我认为你需要的是某种复选框或无线电控制,可供男性或女性选择。也许列表可以完成这项工作。
如果您的计算有效,只需进行完整性检查,将if(male.isPressed())
更改为if(true)
。这将始终返回男性的分数。你也可以为女性班级做同样的事。
另外,如上面的帖子所述,您的子类不需要扩展Activity
类。这当然基于您提供的代码。