更新*我已经删除了女性、男性按钮并按照建议添加了一个复选框,但现在它又崩溃了*。没有语法错误。为什么会崩溃???请帮忙。
这是代码:我的代码有几个问题,如果您能帮助我,我将不胜感激...
我有一个 USMC 初始强度测试计算器,它没有显示任何语法错误。问题是当我在edittext中输入所有值后,当我单击计算时它会崩溃。所以我意识到,这与我希望通过其中一种方法计算的分数显示在 textView 中有关,所以我必须将 textView 转换为 Integer 对吗?而且我还想根据计算的内容在不同的文本视图中显示通过或失败的文本,该方法返回一个字符串。如何显示返回的字符串和返回的整数并将其显示在文本视图上?
这是我所拥有的:
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");
}
}
这是日志:
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: 线程退出未捕获异常 (group=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): 在 android.view.View$1.onClick(View.java:2072)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 android.view.View.performClick(View.java:2408)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 android.view.View$PerformClick.run(View.java:8816)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 android.os.Handler.handleCallback(Handler.java:587)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 android.os.Handler.dispatchMessage(Handler.java:92)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 android.os.Looper.loop(Looper.java:123)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 java.lang.reflect.Method.invoke(Method.java:521)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 22:00:22.037: E/AndroidRuntime(266): at dalvik.system.NativeStart.main(Native Method)
07-30 22:00:22.037:E/AndroidRuntime(266):由:java.lang.reflect.InvocationTargetException 引起
07-30 22:00:22.037: E/AndroidRuntime(266): 在 siuc.cs.rosie.USMCISTActivity.calculateScore(USMCISTActivity.java:61)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 java.lang.reflect.Method.invoke(Method.java:521)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 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): 在 java.lang.Integer.parseInt(Integer.java:412)
07-30 22:00:22.037: E/AndroidRuntime(266): 在 java.lang.Integer.parseInt(Integer.java:382)
07-30 22:00:22.037: E/AndroidRuntime(266): ... 还有 15 个
这是其余的课程:
男班:
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