首先,我是初学者。
我正在遵循本指南:
https://www.youtube.com/watch?v=T7Z4GVFaT4A
然后当我完成对此视频的编程。我收到了这个错误:
FATAL EXCEPTION: main
Process: com.spotcrime.loginregister, PID: 5951
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.spotcrime.loginregister.RegisterActivity$1.onClick(RegisterActivity.java:40)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
现在这是我的两个活动的代码。
RegisterRequest.java
package com.spotcrime.loginregister;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class RegisterRequest extends StringRequest {
//allow us to make request to php file from server and get reponse as a string
//specify where our url of php file is
private static final String REGISTER_REQUEST_URL = "http://spotcrime.net23.net/Register.php";
private Map<String, String> params;
//create constructor for class (first method that runs when instance of this class created
public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener){
//pass data to volley
super(Method.POST,REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("name", name);
params.put("username", username);
params.put("password", password);
params.put("age", age + ""); //age + "" converts from int to string
}
@Override
public Map<String, String> getParams() {
return params;
}
}
RegisterActivity.java
public class RegisterActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//final variable because it wont be assign to anything else after this
//find ageregister cask it to edittext
final EditText etAge = (EditText) findViewById(R.id.etAgeRegister);
final EditText etName = (EditText) findViewById(R.id.etNameRegister);
final EditText etUserName = (EditText) findViewById(R.id.etUsernameLogin);
final EditText etPassword = (EditText) findViewById(R.id.etPasswordRegister);
final Button bRegister = (Button) findViewById(R.id.bRegisterRegister);
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name=etName.getText().toString();// text to string
final String username=etUserName.getText().toString();// text to string
final int age=Integer.parseInt(etAge.getText().toString());// text to string
final String password=etPassword.getText().toString();// text to string
//need to create response listener
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
我不知道是什么问题。我在互联网上研究过。当我构建它时,我没有错误。我假设它指向空值但不确定。