我正在尝试在我的 Android 应用程序中建立一些网络。

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

在我的 fragment 中,如果我的 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 只是 MyApplication 类的一个实例,它扩展了 Application,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(); 时似乎发生了错误

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

这是我的日志:
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

最佳答案

请确保您已在 list 中将 MyApplication 类声明为 android:name="packagename.MyApplication" in application 标签

关于android - 尝试在 Application 类的实例上调用 getApplicationContext() 导致空对象引用错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30419969/

10-11 14:24