0

Main Activity Class Hello guys I am tryin to parse data and I get a java.lang. null exception at return sSingleton.getApplicationContext(); Therefore, can you please help me?

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

}

I also set up a singleton, with a singleton application and a 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;
}

}

Matt
  • 74,352
  • 26
  • 153
  • 180
eli
  • 335
  • 1
  • 3
  • 18
  • If you extend Application, you have to declare it in your manifest – OneCricketeer Feb 14 '16 at 14:47
  • Possible duplicate of [Extending Application to share variables globally](http://stackoverflow.com/questions/4572338/extending-application-to-share-variables-globally) – OneCricketeer Feb 14 '16 at 14:49
  • 1
    Please don't vandalize your post. You can flag it and request it to be disassociate from your account if you wish. – Undo Feb 18 '16 at 05:37

2 Answers2

1

Remove the MyApplicationSingleton class and try something like this:

 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;
    }
cherif
  • 1,164
  • 1
  • 11
  • 16
  • Why remove the Application? I would have moved the VolleySingleton into the Application class – OneCricketeer Feb 14 '16 at 14:47
  • From Google doc: "There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way" (http://developer.android.com/intl/es/reference/android/app/Application.html) – cherif Feb 14 '16 at 15:00
  • Gotcha. I normally just default to the Application extensions because throwing around singletons is bad practice – OneCricketeer Feb 14 '16 at 15:05
0

Assuming you declared the Application in the manifest using android:name=". MyApplicationSingleton" within the application tag, then this method is unnecessary.

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

The application context is MyApplicationSingleton because Application extends Context in Android.

Calling this method on an undeclared Application from missing the manifest definition, though, will throw a NullPointerException


Also, it sounds like you didnt completely follow the README

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245