gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:17 25 4
gpt4 key购买 nike

我正在尝试在我的 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 只是扩展 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();

时似乎发生了错误

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

这是我的日志:

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

最佳答案

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com