简体   繁体   English

使用Singleton模式的Android Volley错误

[英]Android Volley error using Singleton pattern

I am trying to follow this guide on how to work with Volley using a Singleton. 我正在尝试按照本指南使用Singleton如何使用Volley。 The goal is to use this Singleton to have my RequestQueue in an Application context so it won't be affected by shifting from landscape to portrait and such. 目标是使用此Singleton将我的RequestQueue放在Application上下文中,这样它就不会受到从landscape到portrait等的影响。

But I get the following error: 但是我收到以下错误:

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) ... java.lang.NullPointerException:尝试在com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:45)上的空对象引用上调用虚方法'java.io.File android.content.Context.getCacheDir()' )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)在se.ccconsulting.arenaui3.VolleySingleton.getInstance(VolleySingleton.java:34)at se.ccconsulting.arenaui3.AdventureFragment.onCreateView(AdventureFragment.java:62)...

And it points towards this line: 它指向这一行:

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

I am not sure what is wrong here as from what I can see in the code the getInstance() method in VolleySingleton.java is not supposed to 我不确定这里有什么问题,因为我在代码中可以看到VolleySingleton.java中的getInstance()方法不应该

VolleySingleton.java 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 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;
    }
}

This is the line of code I used from Volley before implementing the Singlton which worked perfectly but do not meet my needs: 这是我在实施Singlton之前从Volley使用的代码行,它完美地工作但不满足我的需求:

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

I am debugging on Genymotion. 我在Genymotion上调试。 Fruthermore, row of code from Volley.java mentioned in the exception: Fruthermore,Volley.java中的一行代码在异常中提到:

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

I need help getting passed this error. 我需要帮助才能传递此错误。

If you read the README of the Github repo that code links to, it specifically mentions, you need to add this to your manifest XML. 如果您阅读了代码链接到的Github repo的README,它特别提到,您需要将它添加到清单XML中。

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

</application>

In your case, change com.company.MyApplication to xxx.yyy.HelperApplication , though. 在您的情况下,将com.company.MyApplication更改为xxx.yyy.HelperApplication

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

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