为什么我的Android应用程序强制关闭

时间:2011-07-26 03:39:52

标签: java android

我将把我的代码放在说明中。代码是解决毕达哥拉斯定理

  package com.example;
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 PythagoreantheoremActivity<textField> extends Activity {
    /** Called when the activity is first created. */
    EditText field1;
    EditText field2;
    TextView text1;
    String num1;
    String num2;
    String RAnswer;
    double num3;
    double num4;
    double num5;
    double num6;
    double fnum;
    double snum;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button1 = (Button) findViewById(R.id.button1);
        field1 = (EditText) findViewById(R.id.tf1);
        text1 = (TextView) findViewById(R.id.text1);
        field2 = (EditText) findViewById(R.id.tf2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override   
            public void onClick(View v) {       
                text1.setText(RAnswer);
                double fnum = Double.parseDouble(num1);
                double snum = Double.parseDouble(num2);
                String RAnswer = Double.toString(num6);
                num2 = field1.toString();
                num1 = field2.toString();
                num3 = fnum * fnum;
                num4 = snum * snum; 
                num5 = num4 + num3;
                num6 = sqrt(num5);
            }

            private double sqrt(double num5) {
                // TODO Auto-generated method stub
                return num6;
            }
        });
}

}

这是我的清单代码

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TheoremappActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

2 个答案:

答案 0 :(得分:0)

尝试将sqrt()方法移到setOnClickListener()方法之外。

button1.setOnClickListener(new View.OnClickListener() {
        @Override   
        public void onClick(View v) {       
            text1.setText(RAnswer);
            double fnum = Double.parseDouble(num1);
            double snum = Double.parseDouble(num2);
            String RAnswer = Double.toString(num6);
            num2 = field1.toString();
            num1 = field2.toString();
            num3 = fnum * fnum;
            num4 = snum * snum; 
            num5 = num4 + num3;
            num6 = sqrt(num5);
        }


    });
private double sqrt(double num5) {
            // TODO Auto-generated method stub
            return num6;
        }

答案 1 :(得分:0)

使用Math类访问平方根。 Math.sqrt(number)我认为这就是名字。

如果您想从您所查看的视图中获取文字:

         num2 = field1.getText().toString(); 
        num1 = field2.getText().toString();

您需要在此处进行大量解析,请使用Double.valueOf(yourstring);