Android button needs Multiple clicks
i am creating an app. and i am relativity new. if i try to make the button do its action it will take multiple clicks. for example right now if i press it i will get a blank text view. then if i press it a second time i get a message saying null on the text View. then when i press the button one more time it will finally put out the data.(the code is to add three things together). if some one could help that would be very much appreciated. thank you
the code goes on but this
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 TestingActivity<textField> extends Activity {
/** Called when the activity is first created. */
EditText field1;
EditText field2;
TextView text1;
EditText field3;
EditText field4;
String fnum;
String snum;
String tnum;
String ftnum;
String RAnswer;
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
double num7;
double num8;
double num9;
double num10;
double num11;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
se开发者_StackOverflow中文版tContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
field1 = (EditText) findViewById(R.id.tf1);
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
field2 = (EditText) findViewById(R.id.tf2);
field3 = (EditText) findViewById(R.id.tf3);
field4 = (EditText) findViewById(R.id.tf4);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text1.setText(RAnswer);
fnum = field1.getText().toString();
snum = field2.getText().toString();
tnum = field3.getText().toString();
ftnum = field4.getText().toString();
RAnswer = Double.toString(num6);
num1 = Double.parseDouble(fnum);
num2 = Double.parseDouble(snum);
num3 = Double.parseDouble(tnum);
num4 = Double.parseDouble(ftnum);
RAnswer = Double.toString(num11);
num9 = num2 - num1;
num10 = num4 - num3;
num11 = num10 / num9;`
The end of your onClick method appears to be missing, but I noticed that
text1.setText(RAnswer);
is the first line. I'm guessing that it should be after
RAnswer = Double.toString(num11);
精彩评论