方法不会覆盖其超类的方法

时间:2017-11-25 20:46:39

标签: android google-form

public class MainActivity extends AppCompatActivity {
    View mainView;
    public static final MediaType FORM_DATA_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
    public static final String URL_FORM = "https://docs.google.com/forms/d/e/1FAIpQLSfjuEkX36zN1yycs7qCmnA-fqX7o8VNMEc3_xpNaBunPaVvhg/viewform";

    public static final String NAME_KEY = "entry.1874645379";
    public static final String FATHER_KEY = "entry.500977692";
    public static final String EMAIL_ID = "entry.325007106";
    public static final String CONTACT_NO = "entry.1124073934";
    public static final String WHATSAPP_NO = "entry.1503989573";
    public static final String ADHARCARD_NO = "entry.1176691600";

    public String Blood_group[] = {"O+", "O-", "A+", "A-", "B+", "B-", "AB+", "AB-"}, blood_group;
    public String State[] = {"Andhra Pradesh", "Uttar Pradesh"}, state;

    private Context context;
    private EditText editName;
    private EditText editEmail;
    private EditText editContactNumber;
    private EditText editFather;
    private EditText whatsappno;
    private EditText adharno;
    private TextView select;
    private RelativeLayout layout;
    ArrayAdapter<String> Adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mainView = inflater.inflate(R.layout.activity_main, container, false);
        mainView.setTag("FOUR");

        context = mainView.getContext();
        Button SubmitButton = (Button)mainView. findViewById(R.id.button_register);
        editName = (EditText)mainView. findViewById(R.id.editText_register_name);
        editEmail = (EditText)mainView. findViewById(R.id.editText_register_email);
        editContactNumber = (EditText)mainView. findViewById(R.id.editText_register_phone);
        editFather = (EditText)mainView. findViewById(R.id.editText_register_college);
        whatsappno=(EditText)mainView.findViewById(R.id.editText_register_college);


    }

    class PostDataTask extends AsyncTask<String, Void, Boolean> {
        ProgressDialog progress;
        @Override
        protected void onPreExecute() {
            progress = new ProgressDialog(context);
            progress.setMessage("Please Wait..");
            progress.show();
        }

        @Override
        protected Boolean doInBackground(String... contactData) {
            Boolean result = true;
            String url = contactData[0];
            String name = contactData[1];
            String email = contactData[2];
            String number = contactData[3];
            String whatsappno = contactData[4];
            String editFather = contactData[5];
            String adharno = contactData[6];

            String postBody = "";

            try {

                postBody = NAME_KEY + "=" + URLEncoder.encode(name, "UTF-8") +
                        "&" + EMAIL_ID + "=" + URLEncoder.encode(email, "UTF-8") +
                        "&" + CONTACT_NO + "=" + URLEncoder.encode(number, "UTF-8")+
                        "&" + WHATSAPP_NO + "=" + URLEncoder.encode(whatsappno, "UTF-8")+
                        "&" + FATHER_KEY + "=" + URLEncoder.encode(editFather, "UTF-8")+
                        "&" + ADHARCARD_NO + "=" + URLEncoder.encode(adharno, "UTF-8");

            } catch (UnsupportedEncodingException ex) {
                result = false;
            }
            try {
                OkHttpClient client = new OkHttpClient();
                RequestBody body = RequestBody.create(FORM_DATA_TYPE, postBody);
                Request request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();
                Response response = client.newCall(request).execute();
            } catch (IOException exception) {
                result = false;
            }
            return result;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            progress.cancel();
            final AlertDialog.Builder alert=new AlertDialog.Builder(context);
            alert.setMessage(result ? "Successfully Registered!" : "There was some error in sending message. Please try again after some time.").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    editName.setText("");
                    editContactNumber.setText("");
                    editEmail.setText("");
                    editFather.setText("");
                    whatsappno.setText("");
                    adharno.setText("");



                }
            });
            AlertDialog alertDialog=alert.create();
            alertDialog.show();
        }
    }

    boolean validData()
    {   String userName=editName.getText().toString();
        String userContactNumber = editContactNumber.getText().toString();
        String userEmail = editEmail.getText().toString();
        String userFather=editFather.getText().toString();
        String whatsapp= whatsappno.getText().toString();
        String adhar=adharno.getText().toString();

        if (userName.length()<3)
        {
            Toast.makeText(context, "Enter a Valid Name", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (userContactNumber.length()!=10||userContactNumber.startsWith("0")||userContactNumber.startsWith("1")||userContactNumber.startsWith("2")||userContactNumber.startsWith("3")||userContactNumber.startsWith("4")||userContactNumber.startsWith("5")||userContactNumber.startsWith("6"))
        {
            Toast.makeText(context, "Enter a Valid Number", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (userEmail.length()<3)
        {
            Toast.makeText(context, "Enter a Valid Email Address", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (userFather.length()<3)
        {
            Toast.makeText(context, "Enter a Valid  Name", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(whatsapp.length()!=10|| whatsapp.startsWith("0")||whatsapp.startsWith("1")||whatsapp.startsWith("2")||whatsapp.startsWith("3")||whatsapp.startsWith("4")||whatsapp.startsWith("5")||whatsapp.startsWith("6"))
        {
            Toast.makeText(context, "Enter a Valid Number", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (adhar.length()!=12)
        {
            Toast.makeText(context,"Enter the Valid Adhar Number",Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

它是因为它是AppCompatActivity的覆盖方法,它是片段的覆盖方法。

更改此方法

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

}

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context =this;
        Button SubmitButton = (Button) findViewById(R.id.button_register);
        editName = (EditText)findViewById(R.id.editText_register_name);
        editEmail = (EditText) findViewById(R.id.editText_register_email);
        editContactNumber = (EditText) findViewById(R.id.editText_register_phone);
        editFather = (EditText) findViewById(R.id.editText_register_college);
        whatsappno=(EditText)findViewById(R.id.editText_register_college);
}