我一直在关注教程(http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/),在运行注册代码时会遇到nullpointerexception。
02-11 16:40:57.795: E/AndroidRuntime(1024): java.lang.NullPointerException
02-11 16:40:57.795: E/AndroidRuntime(1024): at activity.RegisterActivity.registerUser(RegisterActivity.java:209)
以下代码行是导致问题的原因
ApplicationController.getInstance().addToRequestQueue(strReq, tag_string_req);
因为当我发表评论时,所有内容都会发现 - 除了它仍然停留在“注册微调器”之外的事实
以下代码是 RegisterUser 方法。
StringRequest strReq = new StringRequest(Method.POST,ApplicationServicesConfig.Register_URL, 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) {
JSONObject user = jObj.getJSONObject("user");
String id = user.getString("id");
String name = user.getString("name");
String email = user.getString("email");
String dob = user.getString("dob");
String gender = user.getString("gender");
String created_at = user.getString("created_at");
db.addUser(name, email, dob, gender, created_at);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
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, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("dob", dob);
params.put("gender", gender);
params.put("password", password);
Log.d(TAG, "params: " + params);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
// Adding request to request queue
ApplicationController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
我最初的想法是,在应用程序控制器类中,我从未真正创建请求队列(因此空指针异常)。
虽然在.addToRequestQueue中,但是创建了一个。
供参考的代码:
public class ApplicationController extends Application {
public static final String TAG = ApplicationController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static ApplicationController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized ApplicationController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
Log.d(TAG, "request queue" + mRequestQueue);
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
Log.d(TAG ,"Within top request queue");
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
Log.d(TAG ,"Within bottom requeuest queue");
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
我也(如你所见)在Volley功能中放置了一些Logging,但永远不会被调用。
我希望我只是无知,
有什么想法吗?
答案 0 :(得分:1)
感谢@NguyenQuangAnh,我意识到我没有指定ApplicationController是应用程序类。
我也查看了这个页面http://rominirani.com/android-application-class/,我在那里学到了更多。
基本上:更新了android清单
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/icon"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:name="app.ApplicationController" >
(在指定活动之前等)