1

更新*我已经删除了女性、男性按钮并按照建议添加了一个复选框,但现在它又崩溃了*。没有语法错误。为什么会崩溃???请帮忙。

这是代码:我的代码有几个问题,如果您能帮助我,我将不胜感激...

我有一个 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
4

3 回答 3

1
m = new maleIst();
f = new femaleIst();

你不能也绝不能尝试这样做。

扩展的类Activity永远不能使用new. AnActivity是一个特例类,只能由操作系统使用startActivity(...)或其他startXXX方法创建。

AnActivity永远不应该公开任何方法或成员变量以供任何其他应用程序组件使用 - Android 根本无法以这种方式工作。

请阅读应用程序基础活动文档。

编辑:首先,extends Activity从您的maleIstfemaleIst类定义中删除。它们显然是帮助类,不提供Activity功能,即它们不用作 UI 组件。

其次,使您的应用程序崩溃的异常是由这一行引起的......

int score = Integer.parseInt(txtscore.getText().toString());

...但是您永远不会为txtscore小部件设置任何文本。在我看来,您要么需要完全删除这一行,要么将其移到代码的后面。

老实说,我真的不确定您的代码在做什么,但看起来您遇到了流程问题,即您的操作顺序错误。

于 2012-07-30T22:27:31.450 回答
0
07-30 22:00:22.037: E/AndroidRuntime(266): Caused by: java.lang.NumberFormatException: unable to parse '' as integer

错误的上述部分暗示您在代码中的某处试图将空字符串转换为数字。我希望它在您MistStateFistState代码中的某个地方。你能把那个贴出来吗?

- 编辑

查看您的额外代码,当字段未完成时,似乎引发了异常。您正在尝试将空字符串转换为整数。

于 2012-07-30T22:16:35.030 回答
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()) {
......
}

由于您单击“计算”按钮来获取分数,因此其他按钮都不会处于按下状态。我相信您在这里需要的是某种复选框或无线电控件,可以从男性或女性中进行选择。也许一份清单可以完成这项工作。

只是为了检查您的计算是否有效,请更改if(male.isPressed())if(true). 这将始终返回男性的分数。你也可以对女班做同样的事情。

此外,如上述帖子中所述,您的子类不需要扩展Activity该类。这当然是基于您提供的代码。

于 2012-07-30T23:10:58.587 回答