尝试使用Volley单例获取NullPointerException

时间:2016-02-14 11:58:38

标签: java android android-volley

主要活动类 大家好我试着解析数据,我得到一个java.lang。返回时返回null异常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;

import 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 Class

 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;
}

}

2 个答案:

答案 0 :(得分:1)

删除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;
    }

答案 1 :(得分:0)

假设您使用android:name=". MyApplicationSingleton"标记内的application在清单中声明了应用,则此方法是不必要的。

public static Context getAppContext(){
    return sSingleton.getApplicationContext();
}

应用程序上下文 MyApplicationSingleton,因为Android中的Application extends Context

在未声明的应用程序上调用此方法会丢失清单定义,但会抛出NullPointerException

此外,听起来你并没有完全遵循README