有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Volley库时的java Android NullPointerException

我一直在遵循一个教程(http://www.安卓hive.info/2012/01/安卓-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);

因为当我对它进行评论时,除了它停留在“注册微调器”上之外,所有东西都会运行find-well

下面的代码是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);
        }
    }
}

我还(如您所见)在截击功能中放置了一些日志记录,但是从未调用过

我希望我只是无知

有什么想法吗


共 (1) 个答案

  1. # 1 楼答案

    多亏了@NguyenQuangAnh,我意识到我没有指定ApplicationController是applications类

    我也看了这一页http://rominirani.com/android-application-class/,在那里我学到了更多

    基本上:更新的安卓清单

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@drawable/icon"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:name="app.ApplicationController" >
    

    (在指定活动等之前)