gpt4 book ai didi

java - 尝试使用 Volley 单例时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 10:25:03 24 4
gpt4 key购买 nike

主要 Activity 类别大家好,我正在尝试解析数据,我得到了一个 java.lang。返回 sSingleton.getApplicationContext() 时出现空异常;因此,您能帮助我吗?

public class MainActivity extends AppCompatActivity {


private VolleySingleton mVolleySingleton;
private RequestQueue mRequestQueue;
private ArrayList<ParseMe> listblogs = new ArrayList<>();
private static final String URL_GET="bestUrl";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final TextView ChangeMe = (TextView) findViewById(R.id.ChangeMeTextView);
Button SunnyButton = (Button) findViewById(R.id.SunnyButton);
Button FoggyButton = (Button) findViewById(R.id.FoggyButton);
setSupportActionBar(toolbar);

mVolleySingleton = VolleySingleton.getInstance();
mRequestQueue = mVolleySingleton.getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL_GET, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ToastTest.m(this.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

}

我还设置了一个单例,带有一个单例应用程序和一个volleysingleton

import android.app.Application;

导入android.content.Context;

public class MyApplicationSingleton extends Application{
private static MyApplicationSingleton sSingleton;
@Override
public void onCreate() {
super.onCreate();
sSingleton=this;
}
public static MyApplicationSingleton getSingleton(){
return sSingleton;
}
public static Context getAppContext(){
return sSingleton.getApplicationContext();
}

}VolleySingleton 类

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

private VolleySingleton() {

mRequestQueue = Volley.newRequestQueue(MyApplicationSingleton.getAppContext());
mImageLoader = 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);

}
});

}

//if our object is equal to null we are going to want to create a new instance of it
public static VolleySingleton getInstance() {
if (sSingleton == null) {
sSingleton = new VolleySingleton();
}
return sSingleton;
}

public RequestQueue getRequestQueue() {
return mRequestQueue;
}

public ImageLoader getImageLoader() {
return mImageLoader;
}

}

最佳答案

删除 MyApplicationSingleton 类并尝试如下操作:

 private static VolleySingleton ourInstance;
private ImageLoader imageLoader;
private RequestQueue requestQueue;
private static Context context;

public static synchronized VolleySingleton getInstance(Context context) {
if (ourInstance == null) {
ourInstance = new VolleySingleton(context.getApplicationContext());
}
return ourInstance;
}

private VolleySingleton(Context context) {
VolleySingleton.context = context;
requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final 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 RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
}
return requestQueue;
}

关于java - 尝试使用 Volley 单例时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391472/

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