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

标签 java android android-volley

主要 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/

相关文章:

java - 为什么我的扫描仪仍然需要一个空格字符

android - 我如何在listview android中切换三个以上的图像

android - 具有泛型和 Void/Nothing 返回类型的 Kotlin 结果类型

C# Volley 如何获取 OnResponse?

android - 如何使用 Volley 将 JSON 对象发送到服务器

java - Java 中的 AES-256-CBC

Java - 重写>variable<中的paint(Graphics g)方法

java - 为什么在 HashTable ADT 的实现中有一个嵌套类(来自教科书的代码)?

java - 我可以在另一个类中发送自定义事件或调用另一个方法而不等待结果吗?

android - Volley 库 NetworkDispatcher.run 错误