Getting NullPointerException trying to use Volley singleton
主要活动类大家好,我正在尝试解析数据,并且在返回ssingleton.getApplicationContext()时得到java.lang.null异常;因此,您能帮我一下吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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); } |
}
我还设置了一个singleton,它有一个singleton应用程序和一个volleysingleton
1 | import android.app.Application; |
导入android.content.context;
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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类,然后尝试如下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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; } |
假设您在清单中使用
1 2 3 |
应用程序上下文是
但是,在未声明的应用程序上调用此方法以避免缺少清单定义,将引发NullPointerException。
而且,听起来你没有完全遵循自述文件