Android 应用程序子类返回空指针异常

Android 应用程序子类返回空指针异常,android,caching,application-singleton,Android,Caching,Application Singleton,我正在创建一个应用程序,我需要加载所有组件(如按钮、徽标)的图像。。从服务器。我使用了来自fedors Lazylist示例的三个类MemoryCache、FileCache和ImageLoder类 在这里,我们可以将url和视图传递给ImageLoader,并加载、缓存图像等 因此,我只想在整个应用程序中初始化ImageLoder类一次,并在其他活动中重用它,以便在我的MyGlobalClass中为它创建一个对象,并使用下面的应用程序进行扩展 public class MyGlobalClas

我正在创建一个应用程序,我需要加载所有组件(如按钮、徽标)的图像。。从服务器。我使用了来自fedors Lazylist示例的三个类MemoryCache、FileCache和ImageLoder类 在这里,我们可以将url和视图传递给ImageLoader,并加载、缓存图像等

因此,我只想在整个应用程序中初始化ImageLoder类一次,并在其他活动中重用它,以便在我的MyGlobalClass中为它创建一个对象,并使用下面的应用程序进行扩展

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;
    }
}
我在AndroidManifest.xml中创建了
字段作为

<application
    android:name="com.xx.yy.MyGlobalClass"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher">
</application>
它返回了一个空指针异常

我想访问
HomeActivity
中的
ImageLoader
,如下所示:

ImageButton homeBt = (ImageButton) findViewById(R.id.imageButton2);
ImageLoader imgLoader=(ImageLoader)myGClass.getImageLoader();
String[] urls=myGClass.getUrls();
imgLoader.DisplayImage(urls[1], cameraBt);
有人能看到我的代码有什么问题并提出建议吗?我还创建了一个单例字段,但不知道为什么,以及如何使用它。请提供一些例子。
我实际上是在onCreate()之前在activity类中添加了上面的异常行,这可能就是问题所在。但是当我在HomeActivity的onCreate()中添加它时,它给了我类强制转换异常。无法将android.app.Application强制转换为com.xx.yy.MyGlobalClass

实际上我正在创建另一个“应用程序”在AndroidManifest.xml文件中的标记不知不觉地,事实是--android:name=“com.xx.yy.MyGlobalClass”--应该添加到您之前创建的同一个“应用程序”标记(如果有的话), 在对ClassCastException问题的回答中,每个人都提到我们需要在ManifestFile中添加带有“android:name”属性的标记..但没有指定它的位置..,我不得不花了将近半天的时间艰难地学习它, 希望对其他人有帮助谢谢syed

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