2017-02-27 68 views
0

我試圖在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(); 
    } 
} 
+0

發表您的logcat –

+0

呀,如果你需要幫助調試內核崩潰添加您的堆棧跟蹤。 – npace

+0

使用我的手機來調試應用程序,如何從中獲取logcat。我在Activity的工作中嘗試了上述方法,但是在其片段中它已經崩潰了。 –

回答

0

要做到這一點有2種方式:使用此代碼片段

1)EditText et = getActivity().findViewById(R.id.editText)

2)使你的EditText public static在你的活動,初始化它只有在那裏。然後在片段中使用,只需寫YourActivity.edittext.getText().toString()。它將工作

-1
awesomeValidation.addValidation(ET_input_first_name, "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", "this is error message"); 

它的工作完美花花公子