我試圖在Fragment
中添加驗證EditText
,但我的應用程序崩潰。我試圖用getActivity()
代替這個問題仍然面臨同樣的問題。向片段中的Edittext添加驗證
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.basgeekball.awesomevalidation.AwesomeValidation;
import com.basgeekball.awesomevalidation.ValidationStyle;
public class TestFragment extends Fragment implements View.OnClickListener{
EditText ET_input_first_name, ET_input_last_name, ET_input_email, ET_input_contact, ET_comments;
String first_name, last_name, email , contact , comments;
private Button buttonSubmit;
private AwesomeValidation awesomeValidation;
public TestFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
view = inflater.inflate(R.layout.fragment_test, container, false);
ET_input_first_name = (EditText)view.findViewById(R.id.input_first_name);
ET_input_last_name = (EditText)view.findViewById(R.id.input_last_name);
ET_input_email = (EditText)view.findViewById(R.id.input_email);
ET_input_contact = (EditText)view.findViewById(R.id.input_contact);
ET_comments = (EditText)view.findViewById(R.id.comments);
awesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
buttonSubmit = (Button)view.findViewById(R.id.btn_test_drive);
//adding validation to edittexts
awesomeValidation.addValidation(this , R.id.input_first_name, "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", R.string.nameerror);
awesomeValidation.addValidation(this, R.id.input_last_name, "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", R.string.nameerror);
awesomeValidation.addValidation(this, R.id.input_email, Patterns.EMAIL_ADDRESS, R.string.nameerror);
awesomeValidation.addValidation(this, R.id.input_contact, "^[2-9]{2}[0-9]{8}$", R.string.nameerror);
buttonSubmit.setOnClickListener(this);
return view;
}
private void submitForm() {
if (awesomeValidation.validate()) {
Toast.makeText(getActivity(), "Validation Successfull", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View view) {
submitForm();
}
}
發表您的logcat –
呀,如果你需要幫助調試內核崩潰添加您的堆棧跟蹤。 – npace
使用我的手機來調試應用程序,如何從中獲取logcat。我在Activity的工作中嘗試了上述方法,但是在其片段中它已經崩潰了。 –