当它试图获取EditText值时Android应用程序崩溃

时间:2016-10-10 09:14:42

标签: java android xml android-studio

我的Android Studio应用有问题。虽然登录功能正常。我的注册功能有问题。一旦用户尝试成功注册他/她自己,应用程序崩溃(可能是由于NullPointerException)。问题可能在于postParams功能。 无论如何这里是代码:

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();
        }
    }
}}

2 个答案:

答案 0 :(得分:2)

您可能会收到TYPE = InnoDB因为您尝试使用NullPointer-Exceptionparam[2],但您没有将其传递给param[3]

您的修正将是Async-Task

答案 1 :(得分:1)

您传递的只是传递电子邮件和密码,因为您设置了params以获取firstName和lastName。像新TaskRegister().execute(txtEmail.getText().toString(), txtPassword1.getText().toString(),txtFirstName.getText().toString(), txtLastName.getText().toString());

一样更正这一行