I have the following code with no compilation errors, just resulting in an infinite loop... I don't get where the problem is.
When I click the Button, I want to create an object using the values entered in the edittexts and pass them to another activity. But instead, I get a loop.
Each spinner is populated using a string-array from resources/strings. I had some difficulties getting the values here, because at the spinner spTipUtilizator
when "Profesor" is selected, I want spGrupa
to be disabled, and when "Student" is selected, I want spdept
to be disabled, but also this doesn't work. And based on that option will be chosen the next activity which will be opened after clicking the button.
Also, why doesn't it work to set the default date in my calendar? I tried multiple things found here, but none of them worked for me. My Android Studio doesn't show DatePicker
, which I would prefer, only that Calendar.
public class SignInActivity extends Activity {
private Button bSignup;
private EditText etNume, etPrenume, etUsername, etPassword, etConfirmPass, etEmail;
private RadioButton rbMasc, rbFem;
private Spinner spTipUtilizator, spgrupa, spdept;
private ProgressBar progBar;
private Calendar calendar;
private int year, month, day, currentYear, defday, defmonth, defyear;
private List<String> tipUtilizatorList, grupaList;
private ArrayAdapter<String> spinnerArrayAdapterTipU;
private String nume,prenume,username,password,password2,email,masc,fem,sex,tipUser,dept,DataN;
private int grupa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
calendar=Calendar.getInstance();
calendar.set(1990,12,12);
bSignup = findViewById(R.id.buttonSignin);
etNume=findViewById(R.id.et_nume);
etPrenume=findViewById(R.id.et_prenume);
etUsername=findViewById(R.id.et_username);
etPassword=findViewById(R.id.et_password);
etConfirmPass = findViewById(R.id.et_confirmpassword);
etEmail=findViewById(R.id.et_email);
rbMasc=findViewById(R.id.radioButtonMasc);
rbFem=findViewById(R.id.radioButtonFem);
spTipUtilizator=findViewById(R.id.spinnerTipUtilizator);
spgrupa=findViewById(R.id.spinnerGrupa);
spdept=findViewById(R.id.spinnerDepartament);
progBar = findViewById(R.id.progressBar);
tipUtilizatorList=new ArrayList<>(Arrays.asList(getResources().getStringArray((R.array.tipUtilizator))));
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,tipUtilizatorList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTipUtilizator.setAdapter(adapter);
grupaList = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.grupa)));
bSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nume = etNume.getText().toString().trim();
prenume = etPrenume.getText().toString().trim();
username = etUsername.getText().toString().trim();
password = etPassword.getText().toString().trim();
password2 = etConfirmPass.getText().toString().trim();
email = etEmail.getText().toString().trim();
masc = rbMasc.getText().toString().trim();
fem = rbFem.getText().toString().trim();
sex=fem;
tipUser = spTipUtilizator.getSelectedItem().toString().trim();
grupa = Integer.parseInt(spgrupa.getSelectedItem().toString().trim());
dept = spdept.getSelectedItem().toString().trim();
DataN = day+"/"+month+"/"+year;
progBar.setVisibility(View.VISIBLE);
if(rbFem.isChecked()) {
sex=fem;
}
else if(rbMasc.isChecked()) {
sex=masc;
}
spTipUtilizator.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Text = parent.getSelectedItem().toString();
if(Text.equals("Profesor")) {
spgrupa.getSelectedView().setEnabled(false);
spdept.getSelectedView().setEnabled(true);
Intent intent = new Intent(getApplicationContext(),StartProfesorActivity.class);
Profesor prof = new Profesor(nume,prenume,username,password,email,sex,dept,DataN);
intent.putExtra("profesor",prof);
startActivity(intent);
return;
}
else if(Text.equals("Student")) {
spgrupa.getSelectedView().setEnabled(true);
spdept.getSelectedView().setEnabled(false);
Intent intent = new Intent(getApplicationContext(),StartStudentActivity.class);
Student stud = new Student(nume,prenume,username,password,email,sex,grupa, DataN);
intent.putExtra("student",stud);
startActivity(intent);
return;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
}
}
Log:
D/AbsListView: in onLayout changed
D/ViewRootImpl@cab402e[PopupWindow:ad4e773]: MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Not IME target window, ignoring
D/ViewRootImpl@cab402e[PopupWindow:ad4e773]: MSG_RESIZED_REPORT: frame=Rect(266, 72 - 422, 1047) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl@27d7010[SignInActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/ViewRootImpl@cab402e[PopupWindow:ad4e773]: ViewPostIme pointer 0
D/ViewRootImpl@cab402e[PopupWindow:ad4e773]: ViewPostIme pointer 1
D/AbsListView: onTouchUp() mTouchMode : 0
D/ViewRootImpl@cab402e[PopupWindow:ad4e773]: Relayout returned: old=[266,72][422,1047] new=[266,72][422,1047] result=0x1 surface={valid=true 481098420224} changed=false
D/ViewRootImpl@27d7010[SignInActivity]: MSG_WINDOW_FOCUS_CHANGED 1
D/ViewRootImpl@cab402e[PopupWindow:ad4e773]: MSG_WINDOW_FOCUS_CHANGED 0
I/zygote64: Compiler allocated 4MB to compile void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/OpenGLRenderer: prepareTree mLayerUpdateQueue is not empty, size = 1
name = PopupWindow$PopupBackgroundView, size (156, 975)
D/OpenGLRenderer: eglDestroySurface = 0x6ff4459d80
endAllActiveAnimators on 0x6ff591ec00 (DropDownListView) with handle 0x7003b6eb00
D/ViewRootImpl@cab402e[PopupWindow:ad4e773]: dispatchDetachedFromWindow
D/InputEventReceiver: channel '468478f PopupWindow:ad4e773 (client)' ~ Disposing input event receiver.
channel '468478f PopupWindow:ad4e773 (client)' ~NativeInputEventReceiver.
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 0
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 1
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 0
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 1
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 0
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 1
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 0
D/ViewRootImpl@27d7010[SignInActivity]: ViewPostIme pointer 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@54a19af nm : com.example.ancaa.proiectandroidquiz ic=com.android.internal.widget.EditableInputConnection@ba71bc
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection
W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
D/OpenGLRenderer: eglDestroySurface = 0x7003ab7400
D/ViewRootImpl@27d7010[SignInActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
D/ViewRootImpl@27d7010[SignInActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/ViewRootImpl@27d7010[SignInActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=false 0} changed=false
Application terminated.
You shouldn't create the OnItemSelectedListener
inside the OnClickListener
for your sign in button, instead you have to create it outside and save the selected option in a variable.
Then when the user clicks your sign in button, you check witch option was selected and start the correct Activity.
Looking at your code, you could simply save the intent to the next activity, and when the user clicks the button just start the Activity on that intent.
I've put a check for nullity before trying to start that intent, and you could show the user some dialog or Toast if no valid option was selected.
public class SignInActivity extends Activity {
private Button bSignup;
private EditText etNume, etPrenume, etUsername, etPassword, etConfirmPass, etEmail;
private RadioButton rbMasc, rbFem;
private Spinner spTipUtilizator, spgrupa, spdept;
private ProgressBar progBar;
private Calendar calendar;
private int year, month, day, currentYear, defday, defmonth, defyear;
private List<String> tipUtilizatorList, grupaList;
private ArrayAdapter<String> spinnerArrayAdapterTipU;
private String nume,prenume,username,password,password2,email,masc,fem,sex,tipUser,dept,DataN;
private int grupa;
private Intent activityToStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
calendar=Calendar.getInstance();
calendar.set(1990,12,12);
bSignup = findViewById(R.id.buttonSignin);
etNume=findViewById(R.id.et_nume);
etPrenume=findViewById(R.id.et_prenume);
etUsername=findViewById(R.id.et_username);
etPassword=findViewById(R.id.et_password);
etConfirmPass = findViewById(R.id.et_confirmpassword);
etEmail=findViewById(R.id.et_email);
rbMasc=findViewById(R.id.radioButtonMasc);
rbFem=findViewById(R.id.radioButtonFem);
spTipUtilizator=findViewById(R.id.spinnerTipUtilizator);
spgrupa=findViewById(R.id.spinnerGrupa);
spdept=findViewById(R.id.spinnerDepartament);
progBar = findViewById(R.id.progressBar);
tipUtilizatorList=new ArrayList<>(Arrays.asList(getResources().getStringArray((R.array.tipUtilizator))));
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,tipUtilizatorList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTipUtilizator.setAdapter(adapter);
grupaList = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.grupa)));
bSignup.setOnClickListener(v -> {
nume = etNume.getText().toString().trim();
prenume = etPrenume.getText().toString().trim();
username = etUsername.getText().toString().trim();
password = etPassword.getText().toString().trim();
password2 = etConfirmPass.getText().toString().trim();
email = etEmail.getText().toString().trim();
masc = rbMasc.getText().toString().trim();
fem = rbFem.getText().toString().trim();
sex=fem;
tipUser = spTipUtilizator.getSelectedItem().toString().trim();
grupa = Integer.parseInt(spgrupa.getSelectedItem().toString().trim());
dept = spdept.getSelectedItem().toString().trim();
DataN = day+"/"+month+"/"+year;
progBar.setVisibility(View.VISIBLE);
if(rbFem.isChecked()) {
sex=fem;
}
else if(rbMasc.isChecked()) {
sex=masc;
}
if(activityToStart != null)
startActivity(activityToStart);
});
spTipUtilizator.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Text = parent.getSelectedItem().toString();
if(Text.equals("Profesor")) {
spgrupa.getSelectedView().setEnabled(false);
spdept.getSelectedView().setEnabled(true);
activityToStart = new Intent(getApplicationContext(),StartProfesorActivity.class);
Profesor prof = new Profesor(nume,prenume,username,password,email,sex,dept,DataN);
activityToStart.putExtra("profesor",prof);
return;
}
else if(Text.equals("Student")) {
spgrupa.getSelectedView().setEnabled(true);
spdept.getSelectedView().setEnabled(false);
activityToStart = new Intent(getApplicationContext(),StartStudentActivity.class);
Student stud = new Student(nume,prenume,username,password,email,sex,grupa, DataN);
activityToStart.putExtra("student",stud);
return;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With