我创建了一个应用类,现在我正在将其实例用于其他类但我面临上下文无效问题。
我的申请类:
public class CommonForApp extends Application {
private static CommonForApp sInstance;
private Utilities mUtilities = null;
private static SharedPreferences mSharedPreferences;
@Override
public void onCreate() {
super.onCreate();
mSharedPreferences = this.getSharedPreferences(
"com.myandroidapp.musicplayer", Context.MODE_PRIVATE);
mContext=this.getApplicationContext();
mUtilities = new Utilities();
}
public CommonForApp() {
}
public static CommonForApp getInstance() {
if (sInstance == null)
sInstance = new CommonForApp();
return sInstance;
}
}
并在AndroidManifest.xml中声明它:
<application
android:name="com.android.musicplayer.datastorageandarraylistsaccesser.CommonForApp"
android:largeHeap="true"
android:icon="@drawable/ic_launcher"
android:label="Music" android:allowBackup="true">
现在我在其他类中使用它:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mApp=CommonForApp.getInstance();
final List<GlobalSongDetails> s=mApp.getGlobalSongsList();
}
我收到以下错误:
03-02 18:50:41.167: E/AndroidRuntime(16177): FATAL EXCEPTION: main
03-02 18:50:41.167: E/AndroidRuntime(16177): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.android.musicplayer.Fragmentactivity}: java.lang.NullPointerException
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.ActivityThread.access$600(ActivityThread.java:156)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.os.Looper.loop(Looper.java:153)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.ActivityThread.main(ActivityThread.java:5297)
03-02 18:50:41.167: E/AndroidRuntime(16177): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 18:50:41.167: E/AndroidRuntime(16177): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 18:50:41.167: E/AndroidRuntime(16177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-02 18:50:41.167: E/AndroidRuntime(16177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-02 18:50:41.167: E/AndroidRuntime(16177): at dalvik.system.NativeStart.main(Native Method)
03-02 18:50:41.167: E/AndroidRuntime(16177): Caused by: java.lang.NullPointerException
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
03-02 18:50:41.167: E/AndroidRuntime(16177): at com.android.musicplayer.datastorageandarraylistsaccesser.CommonForApp.makeGlobalSongsList(CommonForApp.java:127)
03-02 18:50:41.167: E/AndroidRuntime(16177): at com.android.musicplayer.datastorageandarraylistsaccesser.CommonForApp.getGlobalSongsList(CommonForApp.java:164)
03-02 18:50:41.167: E/AndroidRuntime(16177): at com.android.musicplayer.Fragmentactivity.onCreate(Fragmentactivity.java:156)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.Activity.performCreate(Activity.java:5122)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
03-02 18:50:41.167: E/AndroidRuntime(16177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
03-02 18:50:41.167: E/AndroidRuntime(16177): ... 11 more
但如果我像这样制作实例:
mApp=(CommonForApp) this.getApplicationContext();
我没有收到任何错误。
有谁能解释我在做什么错误?
先谢谢!
答案 0 :(得分:1)
问题是在getInstance()
中使用new
手动创建应用程序对象的实例。你不能这样做。
只有系统实例化应用程序对象。
如果你想使用应用程序的单例,你必须这样使用它:
public class CommonForApp extends Application {
private static CommonForApp sInstance;
…
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
…
}
public static CommonForApp getInstance() {
return sInstance;
}
}
请注意,在极少数情况下(例如在内容提供商中),它可以为null。