Question

I am creating an application where i need to load the images for all the components like buttons, logo.. from the server. I am using three classes MemoryCache,FileCache and ImageLoder class from fedors Lazylist example https://github.com/thest1/LazyList/tree/master/src/com/fedorvlasov/lazylist where we can pass url and View to ImageLoader and load,cache the image etc.

so i want to initialize the ImageLoder class only once in the entire app and reused it in other Activities in order to do that i created a object for it in my MyGlobalClass extented with Application like this.

public class MyGlobalClass extends Application {
    private static Context context;
    private static MyGlobalClass singleton;
    private static ImageLoader imageLoder = new ImageLoader(getAppContext());
    private static String[] urls;

    public MyGlobalClass getInstance() {
        return singleton;
    }

    public void onCreate() {
        super.onCreate();
        singleton = this;
        MyGlobalClass .context = getApplicationContext();

        urls = getResources().getStringArray(R.array.urls);
    }

    public static Context getAppContext() {
        return MyGlobalClass .context;
    }

    public ImageLoader getImageLoader() {
        return imageLoder;
    }

    public String[] getUrls() {
        return urls;
    }
}

I created <appplication> field in AndroidManifest.xml as

<application
    android:name="com.xx.yy.MyGlobalClass"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher">
</application>

But when I am using it in other activities like creating object of global class I am doing this:

MyGlobalClass myGClass = (MyGlobalClass)getApplicationContext(); // null pointer here

It's returning a null pointer exception.

I want to access ImageLoader in HomeActivity like this:

ImageButton homeBt = (ImageButton) findViewById(R.id.imageButton2);
ImageLoader imgLoader=(ImageLoader)myGClass.getImageLoader();
String[] urls=myGClass.getUrls();
imgLoader.DisplayImage(urls[1], cameraBt);

Can anybody see what is wrong with my code and suggest how to do it? I also created a singleton field but don't know why, and how to use it. Please provide some examples. I am actually adding the above exception line in activity class before onCreate(), so may be that is the problem..but when i am adding it in onCreate() in HomeActivity it gives me Class cast Exception..cannot cast to android.app.Application to com.xx.yy.MyGlobalClass

Was it helpful?

Solution

Actually iam creating Another "application" tag in the AndroidManifest.xml file unknowingly, the fact is --android:name="com.xx.yy.MyGlobalClass"-- should be added to same "application" tag which you created before if any, In Answers for the ClassCastException questions,every body mentioned we need to add tag with "android:name" property in the ManifestFile..but didn't specify it where..,i had to learn it the hardway wasting almost half of the day, hope it help to others..Thanks syed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top