gpt4 book ai didi

android - Volley 库中 StringRequest 中的 NullPointer 异常

转载 作者:行者123 更新时间:2023-11-30 01:31:37 27 4
gpt4 key购买 nike

我正在使用以下代码在我服务器上的 sql 数据库中注册用户。

package com.cc.envoycc.activity;

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

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.cc.envoycc.R;
import com.cc.envoycc.app.AppConfig;
import com.cc.envoycc.app.AppController;
import com.cc.envoycc.helper.SQLiteHandler;
import com.cc.envoycc.helper.SessionManager;

import org.json.JSONException;
import org.json.JSONObject;

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

/**
* Created by Harsh Ohri on 2/27/2016.
*/
public class RegisterActivity extends Activity {

private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnRegister;
private Button btnLinkToLogin;
private EditText inputFullName;
private EditText inputEmail;
private EditText inputPassword;
private SQLiteHandler db;
private SessionManager session;
private ProgressDialog pDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLogin);
inputFullName = (EditText) findViewById(R.id.name);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);

pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);

session = new SessionManager(getApplicationContext());
db = new SQLiteHandler(getApplicationContext());

if (session.isLoggedIn()) {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
finish();
}

btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();

if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(), "Please enter the credentials", Toast.LENGTH_LONG).show();
}
}
});

btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
finish();
}
});
}

private void registerUser(final String name, final String email, final String password) {
String tag_string_req = "req_register";
pDialog.setMessage("Registering....");
showDialog();

StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register response :" + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("user");
String email = user.getString("email");
String created_at = user.getString("created_at");

db.addUser(name, email, uid, created_at);

Toast.makeText(getApplicationContext(), "User successfully registered.PLease login now", Toast.LENGTH_LONG).show();

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
finish();

} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Response error:" + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}

private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}'

单击注册按钮时出现空指针异常请帮助我是第一次使用 volley 库

这是日志

`

java.lang.NullPointerException                                                                  at com.android.volley.Request.<init>(Request.java:136)                                                                  at com.android.volley.toolbox.StringRequest.<init>(StringRequest.java:43)                                                                  at com.cc.envoycc.activity.RegisterActivity$5.<init>(RegisterActivity.java:137)                                                                  at com.cc.envoycc.activity.RegisterActivity.registerUser(RegisterActivity.java:97)
at com.cc.envoycc.activity.RegisterActivity.access$300(RegisterActivity.java:32)
at com.cc.envoycc.activity.RegisterActivity$1.onClick(RegisterActivity.java:75)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)

`

最佳答案

在注册按钮的 if 条件中检查是否为 null。姓名、电子邮件或密码为空

 btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();

//Check for null here. Either name, email or password is null
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(), "Please enter the credentials", Toast.LENGTH_LONG).show();
}
}
});

关于android - Volley 库中 StringRequest 中的 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35683954/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com