onclick我得到null

时间:2011-07-31 04:19:51

标签: 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 EquationsActivity extends Activity {
    EditText field1;
    EditText field2;
    EditText field3;
    EditText field4;
    TextView text1;
    TextView text2;
    TextView text3;
    String fnum;
    String snum;
    String tnum;
    String ftnum;
    String RAnswer;
    String Answer;
    String answer;
    double num7;
    double num8;
    double num9;
    double num10;
    double num5;
double num4;
double num3;
double num6;
double num1;
double num2;
double num11;
double num12;
double num13;
double num14;
double num15;
String num16;

@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);
    field2 = (EditText) findViewById(R.id.tf2);
    field3 = (EditText) findViewById(R.id.tf3);
    field4 = (EditText) findViewById(R.id.tf4);
    text1 = (TextView) findViewById(R.id.text1);
    text2 = (TextView) findViewById(R.id.text2);
    text3 = (TextView) findViewById(R.id.text3);
    button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            text1.setText(RAnswer);
            text2.setText(Answer);
            text3.setText(answer + "," + num16);
            fnum = field1.getText().toString();
            snum = field2.getText().toString();
            tnum = field3.getText().toString();
            ftnum = field4.getText().toString();
            RAnswer = Double.toString(num7);
            Answer = Double.toString(num11);
            answer = Double.toString(num14);
            num16 = Double.toString(num15);
            num1 = Double.parseDouble(fnum);
            num2 = Double.parseDouble(snum);
            num3 = Double.parseDouble(tnum);
            num4 = Double.parseDouble(ftnum);
            num5 = num2 - num1;
            num6 = num4 - num3;
            num7 = num6 / num5;

            num8 = Math.pow(num2-num1,2);
            num9 = Math.pow(num4-num3, 2);
            num10 = num8+num9;
            num11 = Math.sqrt(num10);

            num12 = num1+num2;
            num13 = num3+num4;
            num14 = num12/2;
            num15 = num13/2;
        }
});

} }`

1 个答案:

答案 0 :(得分:1)

您确定需要多次点击吗?那里有一些数学可能需要一些时间。要进行调试,请在onClick方法的开头添加此行:

Log.i("button", "onClick called");

现在,当您选择按钮时,如果此日志消息显示在您的LogCat中,则该按钮将响应第一次点击。

修改

您是否尝试过移动某些方法组件?

    @Override
    public void onClick(View v) {
        fnum = field1.getText().toString();
        snum = field2.getText().toString();
        tnum = field3.getText().toString();
        ftnum = field4.getText().toString();

        num1 = Double.parseDouble(fnum);
        num2 = Double.parseDouble(snum);
        num3 = Double.parseDouble(tnum);
        num4 = Double.parseDouble(ftnum);
        num5 = num2 - num1;
        num6 = num4 - num3;
        num7 = num6 / num5;

        num8 = Math.pow(num2-num1,2);
        num9 = Math.pow(num4-num3, 2);
        num10 = num8+num9;
        num11 = Math.sqrt(num10);

        num12 = num1+num2;
        num13 = num3+num4;
        num14 = num12/2;
        num15 = num13/2;

        RAnswer = Double.toString(num7);
        Answer = Double.toString(num11);
        answer = Double.toString(num14);
        num16 = Double.toString(num15);
        text1.setText(RAnswer);
        text2.setText(Answer);
        text3.setText(answer + "," + num16);
    }