Singleton 中的 Volley requestQueue returns null

Volley requestQueue in Singleton returns null

我正在使用 Volley 发出网络请求,我正在使用它,但它工作得很好,直到它突然开始在 requestQueue 上抛出空指针异常。

class VolleySingleton(context: Context) {

    companion object{
        @Volatile
        private var newInstance: VolleySingleton? = null

        fun getInstance(context: Context) =
                newInstance
                    ?: synchronized(this){
                    newInstance
                        ?: VolleySingleton(context).also{
                        newInstance = it
                    }
                }
    }

    private val requestQueue: RequestQueue by lazy{
        Volley.newRequestQueue(context) // throws NullPointer exception
    }

    fun<T> addToRequestQueue(req: Request<T>){
        requestQueue.add(req)
    }
}

我试过这样初始化

private val requestQueue: RequestQueue =
        Volley.newRequestQueue(context) // throws NullPointer exception

但它仍然无法正常工作。

我在应用程序 class 中执行此操作,该应用程序是全局的,并且在应用程序会话的生命周期内保持在范围内。这是在 Java 中,但您可能会看到我正在做的事情而您没有。

public class MyApplication extends Application {

    /**
     * Log or request TAG
     */
    public static final String TAG = "MyApp";

    /**
     * Global request queue for Volley
     */
    private RequestQueue mRequestQueue;

    /**
     * A singleton instance of the application class for easy access in other places
     */
    private static MyApplication sInstance;

    @Override
    public void onCreate() {

        super.onCreate();

        // initialize the singleton
        sInstance = this;

    }

    /**
     * @return MyApplication singleton instance
     */
    public static synchronized MyApplication getInstance() {
        return sInstance;
    }

    /**
     * @return The Volley Request queue, the queue will be created if it is null
     */
    public RequestQueue getRequestQueue() {
        // lazy initialize the request queue, the queue instance will be
        // created when it is accessed for the first time
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }


    /**
     * Adds the specified request to the global queue, if tag is specified
     * then it is used else Default TAG is used.
     * 
     * @param req
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);

        VolleyLog.d("Adding request to queue: %s", req.getUrl());

        getRequestQueue().add(req);
    }

    /**
     * Adds the specified request to the global queue using the Default TAG.
     * 
     * @param req
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> req) {
        // set the default tag if tag is empty
        req.setTag(TAG);

        getRequestQueue().add(req);
    }

    /**
     * Cancels all pending requests by the specified TAG, it is important
     * to specify a TAG so that the pending/ongoing requests can be cancelled.
     * 
     * @param tag
     */
    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

}

然后,您在创建请求对象后在其他 Activity 代码中调用它:

MyApplication.getInstance().addToRequestQueue(postRequest);

您应该使用 context.getApplicationContext() 而不是 context

// Add a request (in this example, called stringRequest) to your RequestQueue.
VolleySingleton.getInstance(context.getApplicationContext()).addToRequestQueue(request)