2011-07-31 54 views
0

好的我想讓我的腳本給我一個答案在第一次點擊,但它需要我多次點擊。onclick我得到空

`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; 
     } 
}); 

} }`

+0

如果以下解決方案有幫助,請考慮標記爲「已接受」。 – Phil

回答

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); 
    } 
+0

我已經試過,我仍然需要多次點擊。是的,它是多次點擊,而不是等待。第一次點擊我收到一條消息說null,第一次點擊null。在2ndclick我得到0然後在3日點擊我得到的答案。 – daniel

+0

@daniel,null,null對第一次點擊有意義,因爲你永遠不會初始化num16或answer。對於第二次點擊,'0,0'是有意義的,因爲你從尚未初始化的雙精度創建字符串(所以它們默認爲0)。之後,由於您的變量已經初始化,所以第三次點擊就起作用了。所以最後,按鈕被點擊,你只需要重新思考算法。 – Phil

+0

@daniel,試試我上面提出的算法。 – Phil