Android 使用 Singleton 模式的 Volley 错误

Android Volley error using Singleton pattern

我正在尝试按照 this guide 了解如何使用 Singleton 处理 Volley。目标是使用此 Singleton 将我的 RequestQueue 放在应用程序上下文中,这样它就不会受到从横向转换为纵向等的影响。

但我收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:45) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:105) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:115) at se.ccconsulting.arenaui3.VolleySingleton.(VolleySingleton.java:20) at se.ccconsulting.arenaui3.VolleySingleton.getInstance(VolleySingleton.java:34) at se.ccconsulting.arenaui3.AdventureFragment.onCreateView(AdventureFragment.java:62) ...

它指向这条线:

mRequestQueue = Volley.newRequestQueue(HelperApplication.getAppContext());

我不确定这里有什么问题,因为我在代码中看到 VolleySingleton.java 中的 getInstance() 方法不应该

VolleySingleton.java

public class VolleySingleton {
    private static VolleySingleton mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private VolleySingleton(){
            mRequestQueue = Volley.newRequestQueue(HelperApplication.getAppContext());
            mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });
    }

    public static VolleySingleton getInstance(){
        if(mInstance == null){
            mInstance = new VolleySingleton();
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue(){
        return this.mRequestQueue;
    }

    public ImageLoader getImageLoader(){
        return this.mImageLoader;
    }
}

HelperApplication.java

public class HelperApplication extends Application{
    private static HelperApplication mInstance;
    private static Context mAppContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        this.setAppContext(getApplicationContext());
    }

    public static HelperApplication getInstance(){
        return mInstance;
    }
    public static Context getAppContext() {
        return mAppContext;
    }
    public void setAppContext(Context mAppContext) {
        this.mAppContext = mAppContext;
    }
}

这是我在实现 Singlton 之前使用的 Volley 代码行,它运行良好但不满足我的需求:

RequestQueue queue = Volley.newRequestQueue(getActivity());

我正在 Genymotion 上调试。 此外,异常中提到的来自 Volley.java 的代码行:

File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

我需要帮助来克服这个错误。

如果您阅读代码链接到 Github repo 的自述文件,它特别提到,您需要将其添加到您的清单 XML。

<application android:name="com.company.MyApplication">

</application>

不过,在您的情况下,请将 com.company.MyApplication 更改为 xxx.yyy.HelperApplication