I am writing an app using Java and I can't understand the following thing: I have this listener:
public class SignUpActivity extends AppCompatActivity {
private Button datePickerButton;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private String mydata;
private TextView myDataShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
datePickerButton = findViewById(R.id.signup_date_picker);
myDataShow = findViewById(R.id.signup_date_show)
datePickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
SignUpActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);
String date = month + "/" + day + "/" + year;
myDataShow.setText(date); /** myDataShow is considered null **/
mydata = date; /** it works*/
Log.d(TAG, "aaaa onDateSet: mm/dd/yyy: " + data);
}
};
}
the problem is that myDataShow is null in my onDateSet method, and i don't understand this, I think it is a scope problem.
Moveover if i try to assign the string date to another string variable declared in onCreate method it works, why I have two different behaviours?
Thanks