简体   繁体   中英

Android app crashes when It's trying to get EditText values

I have a problem with my Android Studio app. While the login function works fine. I have a problem with the Register function. As soon the users tries to successfully register him/herself the app crashes (probably due to a NullPointerException). The problem probably lies in the postParams function. Anyways here is the code:

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;



public class RegisterActivity extends Activity{
private EditText txtEmail;
private EditText txtPassword1;
private EditText txtPassword2;
private EditText txtFirstName;
private EditText txtLastName;
private ProgressDialog m_ProgresDialog;
private AccessServiceAPI m_AccessServiceAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    txtEmail = (EditText)findViewById(R.id.txt_email);
    txtPassword1 = (EditText)findViewById(R.id.txt_pwd1);
    txtPassword2 = (EditText) findViewById(R.id.txt_pwd2);
    txtFirstName = (EditText) findViewById(R.id.txt_first_name);
    txtLastName = (EditText) findViewById(R.id.txt_last_name);
    m_AccessServiceAPI = new AccessServiceAPI();
}
public void btnRegister_Click(View v) {
    //Validate input
    if("".equals(txtEmail.getText().toString())) {
        txtEmail.setError("Email is required!");
        return;
    }
    if("".equals(txtFirstName.getText().toString())) {
        txtFirstName.setError("First name is required!");
        return;
    }
    if("".equals(txtLastName.getText().toString())) {
        txtLastName.setError("Last name is required!");
        return;
    }
    if("".equals(txtPassword1.getText().toString())) {
        txtPassword1.setError("Password is required!");
        return;
    }
    if("".equals(txtPassword2.getText().toString())) {
        txtPassword2.setError("Please retype your password!");
        return;
    }
    if(txtPassword1.getText().toString().equals(txtPassword2.getText().toString())) {
        //exec task register
        new TaskRegister().execute(txtEmail.getText().toString(), txtPassword1.getText().toString());
    } else {
        txtPassword2.setError("Confirm password does not match!");
    }
}

public class TaskRegister extends AsyncTask<String, Void, Integer> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        m_ProgresDialog = ProgressDialog.show(RegisterActivity.this, "Please wait", "Registration processing...", true);
    }

    @Override
    protected Integer doInBackground(String... params) {
        Map<String, String> postParam = new HashMap<>();
        postParam.put("email", params[0]);
        postParam.put("password", params[1]); //There is no First name and Last name params because those two lines of code crashes the app. Yet without it, you can't register.
        postParam.put("first_name", params[2]);
        postParam.put("last_name", params[3]);
        try{
            String jsonString = m_AccessServiceAPI.getJSONStringWithParam_POST(Common2.SERVICE_API_URL, postParam);
            JSONObject jsonObject = new JSONObject(jsonString);
            return jsonObject.getInt("result");
        }catch (Exception e) {
            e.printStackTrace();
            return Common2.RESULT_ERROR;
        }

    }

    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);
        m_ProgresDialog.dismiss();
        if(integer == Common2.RESULT_SUCCESS) {
            Toast.makeText(RegisterActivity.this, "Registration success", Toast.LENGTH_LONG).show();
            Intent i = new Intent();
            i.putExtra("email", txtEmail.getText().toString());
            i.putExtra("password", txtPassword1.getText().toString());
            i.putExtra("first_name", txtFirstName.getText().toString()); //These two lines of code causes the app to crash
            i.putExtra("last_name", txtLastName.getText().toString());
            setResult(3, i);
            finish();
        } else if(integer == Common2.RESULT_USER_EXISTS) {
            Toast.makeText(RegisterActivity.this, "Username already exists!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(RegisterActivity.this, "Registration failed!", Toast.LENGTH_LONG).show();
        }
    }
}}

You probably get a NullPointer-Exception because you try to use param[2] and param[3] but you didn't pass those to the Async-Task .

Your fix would be new TaskRegister().execute(txtEmail.getText().toString(), txtPassword1.getText().toString(), txtFirstName.getText().toString(), txtLastName.getText().toString());

you are passing only passing only email and password where as you set params to take firstName and lastName too. correct the line like new TaskRegister().execute(txtEmail.getText().toString(), txtPassword1.getText().toString(),txtFirstName.getText().toString(), txtLastName.getText().toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM