我按照这个例子:
http://www.intridea.com/blog/2011/5/24/how-to-use-application-object-of-android
似乎它在onCreate()
of MyApplication
when
中崩溃了singleton = this
。
public class MyApplication extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private static MyApplication singleton;
@Override
public void onCreate() {
super.onCreate();
Log.v("MyApplication", "Hi from my application");
singleton = this;
Log.v("MyApplication", "End onCreat of myApplicatoin");
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
@Override
public void onTerminate() {
super.onTerminate();
}
public MyApplication getInstance(){
return singleton;
}
public boolean isNetworkAvailable() {
return true;
}
}
来自 MainActivity 的代码:
MyApplication mApplication = (MyApplication) getApplicationContext();
以下是 logcat 消息:
11-23 16:43:01.398: V/MyApplication(30957): Hi from my application
11-23 16:43:01.398: E/AndroidRuntime(30957): FATAL EXCEPTION: main
11-23 16:43:01.398: E/AndroidRuntime(30957): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jimmyc.fall12.OpenClass/com.jimmyc.fall12.OpenClass.MainInterface}: java.lang.NullPointerException
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.os.Looper.loop(Looper.java:137)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.app.ActivityThread.main(ActivityThread.java:5039)
11-23 16:43:01.398: E/AndroidRuntime(30957): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 16:43:01.398: E/AndroidRuntime(30957): at java.lang.reflect.Method.invoke(Method.java:511)
11-23 16:43:01.398: E/AndroidRuntime(30957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-23 16:43:01.398: E/AndroidRuntime(30957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-23 16:43:01.398: E/AndroidRuntime(30957): at dalvik.system.NativeStart.main(Native Method)
11-23 16:43:01.398: E/AndroidRuntime(30957): Caused by: java.lang.NullPointerException
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
11-23 16:43:01.398: E/AndroidRuntime(30957): at com.jimmyc.fall12.OpenClass.MainInterface.<init>(MainInterface.java:75)
11-23 16:43:01.398: E/AndroidRuntime(30957): at java.lang.Class.newInstanceImpl(Native Method)
11-23 16:43:01.398: E/AndroidRuntime(30957): at java.lang.Class.newInstance(Class.java:1319)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
11-23 16:43:01.398: E/AndroidRuntime(30957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
> 11-23 16:43:01.398: E/AndroidRuntime(30957): ... 11 more
有任何想法吗?
MainInterface 的第 74、75、76 行
74: //////////////访问全局函数
75: 我的应用程序 mApplication = (我的应用程序) getApplicationContext();
76://