0

Here is my code in which on clicking I am calling AttemptLogin method.

class NewActivity extends AppCompatActivity implements View.OnClickListener {


EditText editEmail, editPassword, editName;
Button btnSignIn, btnRegister;
private ProgressDialog pDialog;

String URL= "https://xyz/restapi/registration";

JSONParser jsonParser=new JSONParser();

int i=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    btnRegister=(Button)findViewById(R.id.btnRegister);
    btnRegister.setOnClickListener(this);

    /*btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AttemptLogin attemptLogin= new AttemptLogin();
            attemptLogin.execute(editName.getText().toString(),editPassword.getText().toString(),"");
        }
    });*/

    /*btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new AttemptLogin().execute();
            }


    });*/
}

@Override
public void onClick(View v) {
    new AttemptLogin().execute();
}

private class AttemptLogin extends AsyncTask<String, String, JSONObject> {

    @Override

    protected void onPreExecute() {

        super.onPreExecute();
        pDialog = new ProgressDialog(NewActivity.this);
        pDialog.setMessage("Attempting for registering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override

    protected JSONObject doInBackground(String... args) {





        ArrayList params = new ArrayList();
        params.add(new BasicNameValuePair("cust_firstname", "Sai"));
        params.add(new BasicNameValuePair("cust_lastname", "Kumar"));
        params.add(new BasicNameValuePair("cust_phoneno", "9989219692"));
        params.add(new BasicNameValuePair("cust_confirmpass", "Sai@123"));
        params.add(new BasicNameValuePair("cust_pass", "Sai@123"));
            params.add(new BasicNameValuePair("cust_username", "sai@gmail.com"));

        JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params);


        return json;

    }

    protected void onPostExecute(JSONObject result) {

        // dismiss the dialog once product deleted
        //Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();

        try {
            if (result != null) {
                Toast.makeText(getApplicationContext(),result.getString("message"),Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

}

}

on button click i want to post data directly but getting error as

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

If there are any nice tutorials for registering details for Async task means please let me know.

Naveen T P
  • 6,955
  • 2
  • 22
  • 29
lakshman kumar
  • 341
  • 4
  • 14

3 Answers3

0
@Override
public void onClick(View v) {
    switch(v.getId){
    case R.id.button:
   new AttemptLogin().execute();
 }
}

try this one

Naveen T P
  • 6,955
  • 2
  • 22
  • 29
0

The id of the button is correct? It corresponds with the id in XML layout?

Use in onCreate:

 btnRegister.setOnClickListener(btnLoginClick);

Then, out of the onCreate declare method:

private View.OnClickListener btnLoginClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          ...
          //write your code
        }
    };
A. Ror
  • 17
  • 1
  • 7
0

Try this out.

@Override
public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnRegister: 
              new AttemptLogin().execute();
              break;
            default:
              break;
    }
 }

The official Android Developer site should be your closed friend. Make sure you read this: https://developer.android.com/reference/android/os/AsyncTask

Chez
  • 81
  • 1
  • 6