دارم یک برنامه ماشین حساب ساده برای آندروید مینویسم که برای تعریف متغیر با مشکل مواجه شدم . مشکل اینجاست که در رویداد کلیک دکمه
button0.setOnClickListener میخوام از متغیرهایی که بیرون از تابع تعریف شده استفاده کنم که اجازه نمیده و باید حتما متغیرها رو داخل تابع معرفی کنم در حالیکه در اینصورت برنامه بهم میریزه .
من قبلا همین برنامه رو با جاوا و به همین شکل نوشتم و اونجا همچین اروری نمیداد. لطفا کمکم کنید .
اینم برنامه که هنوز ناقصه :
package test00.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test00Activity extends Activity {
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button buttonClear;
Button buttonEquals;
Button buttonAdd;
Button buttonMinus;
Button buttonMultiply;
Button buttonDivision;
EditText editText1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonEquals = (Button) findViewById(R.id.buttonEquals);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonMinus = (Button) findViewById(R.id.buttonMinus);
buttonMultiply = (Button) findViewById(R.id.buttonMultiply);
buttonDivision = (Button) findViewById(R.id.buttonDivision);
editText1 = (EditText) findViewById(R.id.editText1);
editText1.setText(String.valueOf(0));
private int state = 1, num1, num2, result;
button0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (state == 1)
{
num1 = Integer.parseInt(editText1.getText().toString());
num1 = (num1 * 10) + 0;
editText1.setText(String.valueOf(num1));
}
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (state == 1)
{
num1 = Integer.parseInt(editText1.getText().toString());
num1 = (num1 * 10) + 1;
editText1.setText(String.valueOf(num1));
}
}
});
}
}
اینم عکس از ارور مورد نظر :
54166715545464362771.jpg