尝试在Application类的实例上调用getApplicationContext()会导致null对象引用错误

时间:2015-05-24 04:02:56

标签: android android-volley

我正在尝试在我的Android应用中进行一些网络连接。

导致我的错误的事件序列如下:

在我的片段中,如果还没有,我会获得我的VolleySingleton类的新实例。

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

在创建VolleySingleton的实例时,我创建了一个请求队列并将其设置为等于Volley.newRequestQueue(myApplication.getContext());

    public class VolleySingleton {
    private static VolleySingleton sInstance = null;
    private ImageLoader imageLoader;
    private RequestQueue mRequestQueue;
    MyApplication myApplication = new MyApplication();

    private VolleySingleton() {
        mRequestQueue = Volley.newRequestQueue(myApplication.getAppContext());
        imageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
            private LruCache<String, Bitmap> cache = new LruCache<>((int)(Runtime.getRuntime().maxMemory()/1024/8));
            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
        public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

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

    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }
}

现在,myApplication只是扩展Application的MyApplication类的一个实例,而getContext()只是在MyApplication的实例上调用getApplicationContext。以下是MyApplication类的代码:

public class MyApplication extends Application {

    private static MyApplication sInstance;
    public static final String API_KEY = "xxxxxxxx";

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

    public static MyApplication getInstance() {
        return sInstance;
    }

    public Context getAppContext() {
        return sInstance.getApplicationContext();
    }


}

当我尝试return sInstance.getApplicationContext();

时,似乎发生了错误

我已经设法让相同的代码在一个单独的测试项目中工作,所以我在这里不知所措。

这是我的logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.xxxxxxxxx.chessgame.Models.MyApplication.getApplicationContext()' on a null object reference
            at com.xxxxxxxxx.chessgame.Models.MyApplication.getAppContext(MyApplication.java:28)
            at com.xxxxxxxxxx.chessgame.Networking.VolleySingleton.<init>(VolleySingleton.java:25)
            at com.xxxxxxxxx.chessgame.Networking.VolleySingleton.getInstance(VolleySingleton.java:42)
            at com.xxxxxxxx.chessgame.RecyclerViews.FragmentViewOne.onCreate(FragmentViewOne.java:95)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
            at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:842)
            at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:859)
            at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:152)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:17430)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.Act

2 个答案:

答案 0 :(得分:5)

请确保您已将清单中的MyApplication类声明为 android:name =&#34; packagename.MyApplication&#34; 应用程序标记

答案 1 :(得分:2)

  

MyApplication myApplication = new MyApplication();

似乎错了。

您可以更改VolleySingleton的构造函数和getInstance方法以接受Context作为参数。

Application有onCreate方法表明,它的生命周期是由Android操作系统管理的。你不应该直接调用构造函数,就像你不应该做新的Activity()一样。 您可以从例如获取应用程序的实例Activity#getApplicationContext()