简体   繁体   English

使用Volley库时,Android NullPointerException

[英]Android NullPointerException when using Volley library

I have been following a tutorial ( http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ ) and when running the code for registration a encounter a nullpointerexception. 我一直在关注教程( http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ ),并且在运行注册代码时遇到了空指针异常。

 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)

The following line of code is what causes the issue 以下代码行是导致问题的原因

ApplicationController.getInstance().addToRequestQueue(strReq, tag_string_req);

Because when I comment it out, everything runs find - well apart from the fact it stays stuck on the "registering spinner" 因为当我将其注释掉时,所有运行都可以找到-除了它停留在“注册微调器”上的事实之外

The code below is the RegisterUser method. 下面的代码是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);
    }

My initial thoughts was that within the application controller class I was never actually creating a request queue (hence the null pointer exception). 我最初的想法是,在应用程序控制器类中,我从未真正创建过请求队列(因此​​,空指针异常)。

Although within the .addToRequestQueue, one is created. 尽管在.addToRequestQueue中,但已创建一个。

Code supplied for reference: 提供的代码可供参考:

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

I have also (as you can see) placed some Logging within the Volley functionality, however that never gets called. 我还(如您所见)在Volley功能中放置了一些Logging,但是从未调用过它。

I'm hoping I'm just being ignorant, 我希望我只是无知,

Any ideas? 有任何想法吗?

Thanks to @NguyenQuangAnh I realised that I didn't specify that ApplicationController is applications class. 感谢@NguyenQuangAnh,我意识到我没有指定ApplicationController是应用程序类。

I also looked on this page http://rominirani.com/android-application-class/ where I learn't more. 我还在此页面http://rominirani.com/android-application-class/上浏览了我了解的更多信息。

Basically: Updated android manifest 基本上:更新了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" >

(Before you specify activities etc) (在指定活动等之前)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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