我的简单添加应用程序
中未解决setOnClickListner错误这是我的代码请帮忙解决我的问题谢谢!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumber = (EditText)findViewById(R.id.txtNumber1);
secondNumber = (EditText)findViewById(R.id.txtNumber2);
addResult = (TextView)findViewById(R.id.txtResult);
btnAdd = (Button)findViewById(R.id.btnAdd);
}
EditText firstNumber;
EditText secondNumber;
TextView addResult;
Button btnAdd;
double num1,num2,sum;
final Button mButton = (Button) findViewById(R.id.btnAdd);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 + num2;
addResult.setText(Double.toString(sum));
}
});
}
答案 0 :(得分:2)
您在任何方法之外呼叫或设置import os
.
.
.
f.flush() #flushes internal buffer and copies data to OS buffer
os.fsync(f.fileno()) #the os buffer refers to the file-descriptor(fd=f.fileno()) to write values to disk
f.write(line)
。
将代码放在方法或setOnClickListner
方法中。
尝试以下代码,它将解决您的问题
onCreate()
答案 1 :(得分:0)
根据您的代码,您在override方法之外编写了按钮侦听器代码。 所以在下面给出的方法中编写代码:
public class MainActivity extends AppCompatActivity {
TextView textViewSum;
EditText editFirst, editSecond;
Button buttonSum;
double num1,num2,sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFirst = (EditText) findViewById(R.id.edtFirst);
editSecond = (EditText) findViewById(R.id.edtSecond);
textViewSum = (TextView) findViewById(R.id.textViewSum);
buttonSum = (Button) findViewById(R.id.buttonSum);
buttonSum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Double.parseDouble(editFirst.getText().toString());
num2 = Double.parseDouble(editSecond.getText().toString());
sum = num1 + num2;
textViewSum.setText(Double.toString(sum));
}
});
}
}